Can't tap SKSpriteNode - no touch detected ios

后端 未结 4 569
萌比男神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 08:57

    I never used the SKSprite framework but I will try to help. First, your function "-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event" has to be set in your custom SKSpriteNode class, if not it's normal if that doesn't work.

    If it's already done, you have to search where the tap is handle (by an other object) then you can use : "- (BOOL)gestureRecognizer:(UIPanGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer)otherGestureRecognizer"

    hope that will help !

    0 讨论(0)
  • 2021-01-12 09:14

    Your code is essentially correct. Just a few minor changes are required.

    You have implemented the -touchesBegan: method in your scene class. If you want to handle touches from within the scene class, then all the child nodes should have the userInteractionEnabled property set to NO. A sprite node's userInteractionEnabled property should be set to YES only when it is capable of handling touches on it's own.

    So, in your case you should make the following changes:

    1 - Set the spriteNode's userInteraction to NO.

    mySKSpriteNode.userInteractionEnabled = NO;
    

    2 - Change the way you check for your own sprite.

    if([node isEqual: mySKSpriteNode])
    

    You will find your code is working as intended.

    0 讨论(0)
  • 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]];
        }
    }
    
    0 讨论(0)
  • 2021-01-12 09:21

    In Swift 2.0!

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    
      let touch = touches.first!
      let location = touch.locationInNode(self)
      let node = self.nodeAtPoint(location)
    
      if node.name == "spriteName" {
        // Present new game scene
      }
    }
    
    0 讨论(0)
提交回复
热议问题