SKSpriteNode不稳定滑动?

时间:2015-03-15 19:37:34

标签: ios objective-c sprite-kit uiswipegesturerecognizer touchesbegan

我创建了以下精灵工具包节点:

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

并添加了滑动手势,如下所示:

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

当我滑动节点时,它会滑动但它会回到原来的位置然后淡化以移除。

我想要的是,当我滑动时,它应移动到滑动位置并移除。我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)

我使用了[SKAction moveByX:y:]而不是[SKAction moveToY:duration:],这使得一切都像魅力一样

以下是更新的handleSwipeUp:方法:

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

}

希望这有帮助!!!