【问题标题】:How to repeat action as long I touch and when I stop touching the action stop in SpriteKit Swift只要我触摸以及当我停止触摸 SpriteKit Swift 中的动作停止时,如何重复动作
【发布时间】:2015-08-03 07:56:15
【问题描述】:

正如标题中所说,我想让一个动作运行并重复,只要我按下一个按钮并在我停止触摸时停止并运行另一个动作。我不知道这样做的方法,所以如果有人可以帮助我编写代码,例如我想要一个精灵 只要我按下就旋转,在我停止触摸后向上移动。

let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1.0)
    sprite.runAction(SKAction.repeatActionForever(action))  
          let action2 = SKAction.moveToY(900, duration: 1.0)
            sprite.runAction(action2)

准确描述我想要的:

当我触摸屏幕时,将创建一个对象并旋转并继续旋转,直到我将手指从屏幕上松开并停止触摸我希望它向上

【问题讨论】:

    标签: ios swift sprite-kit skaction


    【解决方案1】:

    在您的 Sprite Kit 场景中,这些方法允许您检测触摸何时开始和结束:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        // Start an action that repeats indefinitely.
    }
    
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        // Remove the action after touches end.
    }
    

    当触摸结束时,调用self.removeAllActions() 停止场景中的所有动作,或调用sprite.removeAllActions() 停止特定于精灵的动作。

    【讨论】:

    • 感谢您的回复,您能告诉我如何在这些函数之间传递精灵吗?
    • 将方法外的精灵变量声明为类变量。
    【解决方案2】:
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        sprite.removeAllActions()
        let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1.0)
        sprite.runAction(action)
    }
    
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        sprite.removeAllActions()
        let action2 = SKAction.moveToY(900, duration: 1.0)
        sprite.runAction(action2)
    }
    

    【讨论】:

    • 首先感谢您的响应,如果我在旋转精灵的动作之后删除所有动作不会旋转并且只会执行其他动作,也许我没有明确说明我想要什么,我不知道该怎么做但是我会告诉我想要它是什么,“当我触摸屏幕时,我想创建对象并旋转直到我松开手指,我希望它向上”
    猜你喜欢
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多