我在游戏中使用SpriteKit框架。我正在显示一个菜单,现在我需要确定是否单击了SKLabelNode。我怎样才能做到这一点?

 SKLabelNode *startGameLabel = [SKLabelNode labelNodeWithFontNamed:@"Marker Felt"];
    startGameLabel.text = @"Start";
    startGameLabel.position = CGPointMake(0, -40);

    [gameOverScreenBackground addChild:startGameLabel];

最佳答案

这是一个如何做的例子...

#import "MyScene.h"

@implementation MyScene
{
     SKLabelNode *startGameLabel;
}

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
         startGameLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        startGameLabel.fontSize = 40.0;
        startGameLabel.text = @"Start";
        startGameLabel.name = @"start";
        startGameLabel.position = CGPointMake(200,200);
        [self addChild:startGameLabel];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self.scene];

    if (CGRectContainsPoint(startGameLabel.frame, touchLocation))
    {
        NSLog(@"stop touching me!");
    }
}

-(void)update:(CFTimeInterval)currentTime
{
    //
}

@end

关于ios - SpriteKit UILabelNode附加事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22999093/

10-11 19:47