【问题标题】:How can setKey for value be used with SpriteKit?如何将 setKey for value 与 SpriteKit 一起使用?
【发布时间】:2013-12-25 22:56:41
【问题描述】:

我正在用SpriteKit做一个平台游戏,我正在尝试使用setValue:forKey:的方法 在touchesMoved:withEvent 方法内部,因为我一团糟:我需要删除SKAction 类型的动画。我在touchesMoved:withEvent 中运行这个SKAction 动画,并检查所有需要的条件。这些条件之一是:

if(NO ==[[self childNodeWithName:@"hero"] hasActions])
{
     //code to run animation
}

我检查它是否没有动作,因为我不想创建超出需要的动画。 当我用手指触摸屏幕时,动画会一直运行,并且只有当我在touchesEnded:withEvent 方法中将手指从屏幕上移开时它才会移除

所以在我触摸屏幕之前,英雄动画将永远循环播放。我遇到的问题是,如果我将手指从屏幕右侧移动到屏幕左侧,动画仍然是一样的:英雄向右跑,即使精灵向左移动,因为if 检查是否有动作,不要在左边创建新的动画,当然也不要删除右边的动画。

我想我可以修复它,为动画动作设置一个键,并使用 if 和使用手指的位置以相同的方法将其删除。问题是我不知道如何使用removeActionForKey 方法。我试过阅读文档,但我发现它不是很清楚。谁能给我发一些如何使用它的代码示例?

【问题讨论】:

    标签: ios objective-c ios7 sprite-kit


    【解决方案1】:

    你需要使用runAction:withKey:方法:

    // if direction == right
    [node removeActionForKey:@"left"];
    SKAction *animationRight = [SKAction animateWithTextures:......];
    [node runAction:animationRight withKey:@"right"];
    
    ...........
    
    // if direction == left
    [node removeActionForKey:@"right"];
    SKAction *animationLeft = [SKAction animateWithTextures:......];
    [node runAction:animationLeft withKey:@"left"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      相关资源
      最近更新 更多