且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法点击 SKSpriteNode - 未检测到触摸 ios

更新时间:2022-06-01 22:24:08

您是否尝试过设置 spritenode 的 name 属性?

Have you tried setting the name property of your spritenode?

mySKSpriteNode 的初始化中:

mySKSpriteNode = [[SKSpriteNode alloc] initWithTexture:sometexture];
mySKSpriteNode.name = @"thisIsMySprite"; // set the name for your sprite
mySKSpriteNode.userInteractionEnabled = NO; // userInteractionEnabled should be disabled

然后:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if ([node.name isEqualToString:@"thisIsMySprite"]) {
        NSLog(@"mySKSpriteNode was touched!");
        [node runAction:[SKAction fadeOutWithDuration:0]];
    }
}