ios - SKSpriteNode 不稳定滑动?

标签 ios objective-c sprite-kit uiswipegesturerecognizer touchesbegan

我创建了以下 sprite kit 节点:

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

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

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

最佳答案

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

这是更新后的 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];
  }
//}

希望这有帮助!!!

关于ios - SKSpriteNode 不稳定滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065278/

相关文章:

iphone - 如何确定 UINavigationController 中工具栏的高度?

objective-c - 不确定的 NSProgressIndicator 不会设置动画

ios - SpriteKit + iOS8。 SKFieldNode 的电场方向是什么?

ios - Sprite Kit bodyAtPoint 和 bodyInRect 返回不正确的值?

ios - 将闭包附加到 UIButton(设计自定义弹出窗口)

ipad - xcode 中的多个 PDF 页面和二维 ScrollView

ios - 使用不同 viewCell 布局的最佳方式

ios - 类型 'Any' 的值在 swift 4 中没有成员 'text

iphone - NSNumberFormatter 和舍入百分比值

swift - 执行相同的代码行