ios - 如何在 SpriteKit Swift 中只要我触摸就重复操作,当我停止触摸时操作停止

标签 ios swift sprite-kit skaction

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

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)

准确描述我想要的内容:

when I touch the screen an object will be created and rotate and keep rotating till I release my finger from the screen and stop touching I want it to go up

最佳答案

在 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()来停止场景中的所有 Action ,或调用sprite.removeAllActions()来停止特定于 Sprite 的 Action 。

关于ios - 如何在 SpriteKit Swift 中只要我触摸就重复操作,当我停止触摸时操作停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31782150/

相关文章:

ios - 如何将下拉文本值获取到 label.text

iphone - 异步请求和委托(delegate)可能存在的风险

swift - LottieAnimationView 大小不会改变/太小(iOS/Swift)

ios - 如何以及在哪里检测自定义 UIView 大小动态变化并相应地调整 ScrollView ?

ios - 在另一个对象中初始化 UISwipeGestureRecognizer 时使用未声明的标识符 'action'

ios - iOS Spritekit 游戏中的肌酐水平

SpriteKit 的 SKPhysicsBody 和多边形辅助工具

ios - 创建一个提醒列表,它可以在您从我的应用程序创建的提醒应用程序中保存您的提醒

ios - 我无法在 UIViewController 中访问相机

ios - 如何创建面向协议(protocol)的通用服务?