gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:10:52 24 4
gpt4 key购买 nike

正如标题中所说,我想运行一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com