Can't tap SKSpriteNode - no touch detected ios

后端 未结 4 600
萌比男神i
萌比男神i 2021-01-12 08:49

I am pretty new to iOS, objective C and working with Xcode. I only did one simple news type app, but now I would like to create a game I had published on another platform.

4条回答
  •  不要未来只要你来
    2021-01-12 09:19

    Have you tried setting the name property of your spritenode?

    In your initialization for mySKSpriteNode:

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

    Then:

    -(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]];
        }
    }
    

提交回复
热议问题