SKSpriteNode erratic swipe?

落爺英雄遲暮 提交于 2019-12-22 12:39:43

问题


I have created the following sprite kit node :

SKSpriteNode *megadeth;
megadeth = [SKSpriteNode spriteNodeWithImageNamed:@"megadeth_rocks.png"];
megadeth.name = @"awesome";

and added the swipe gesture as follows:

-(void)didMoveToView:(SKView *)view{
   UISwipeGestureRecognizer *recognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUp:)];
recognizerUp.direction = UISwipeGestureRecognizerDirectionUp;
[[self view] addGestureRecognizer:recognizerUp];

UISwipeGestureRecognizer *recognizerDn = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeDn:)];
recognizerDn.direction = UISwipeGestureRecognizerDirectionDown;
[[self view] addGestureRecognizer:recognizerDn];
}

- (void)handleSwipeUp:(UISwipeGestureRecognizer *)sender{

     NSLog(@"Node Swiped Up");
     if (sender.state == UIGestureRecognizerStateEnded)
       {
        CGPoint touchLocation = [sender locationInView:sender.view];
        touchLocation = [self convertPointFromView:touchLocation];
        SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
        if([touchedNode.name isEqualToString:@"awesome"]){
        NSLog(@"Perform Action on AWESOME node");
        SKAction *moveUp = [SKAction moveByX:0.0 y:(0-touchedNode.position.y) duration:0.5];
        SKAction *fade = [SKAction fadeOutWithDuration:0.25];
        //SKAction *remove = [SKAction removeFromParent];
        SKAction *sequence = [SKAction sequence:@[moveUp, fade]];
        [touchedNode runAction:sequence];
      }
    }
}

When I swipe the node, its swiping but it comes back to its original position and then fades to remove.

What I want is that when I swipe, it should move to the swiped position and remove. Am I missing something here ?


回答1:


Instead of [SKAction moveByX:y:] , I used [SKAction moveToY:duration:] which made everything work like charm

Here's the updated handleSwipeUp: method:

- (void)handleSwipeUp:(UISwipeGestureRecognizer *)sender{

 NSLog(@"Node Swiped Up");
 //if (sender.state == UIGestureRecognizerStateEnded)
   //{
    CGPoint touchLocation = [sender locationInView:sender.view];
    touchLocation = [self convertPointFromView:touchLocation];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
    if([touchedNode.name isEqualToString:@"awesome"]){
    NSLog(@"Perform Action on AWESOME node");
    **SKAction *moveUp = [SKAction moveToY:(touchedNode.position.y+200) duration:0.5];**
    //SKAction *fade = [SKAction fadeOutWithDuration:0.25];
    SKAction *remove = [SKAction removeFromParent];
    SKAction *sequence = [SKAction sequence:@[moveUp, remove]];
    [touchedNode runAction:sequence];
  }
//}

}

Hope this helps !!!



来源:https://stackoverflow.com/questions/29065278/skspritenode-erratic-swipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!