【问题标题】:SKSpriteNode erratic swipe?SKSpriteNode 不稳定的滑动?
【发布时间】:2015-03-15 19:37:34
【问题描述】:

我创建了以下精灵套件节点:

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

当我滑动节点时,它会滑动,但它会回到原来的位置,然后逐渐消失。

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

【问题讨论】:

  • 您应该在 SKScene 中使用 locationInNode: 方法而不是 locationInView:。那么你也不需要转换点。
  • @ZeMoon 我在 touchesBegan 方法中使用 locationInNode。手势识别器方法只处理视图中的位置
  • 问题出在[SKAction moveByX:0.0 y:(0-touchedNode.position.y) duration:0.5]
  • 你想要达到的效果是什么>
  • @ZeMoon 只需向上/向下滑动节点,当它即将到达终点时,它会逐渐消失......在短时间滑动以删除......

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


【解决方案1】:

我使用 [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];
  }
//}

}

希望对你有帮助!!!

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多