如何制作随机重复动作的动画?

发布于 2025-01-10 15:38:28 字数 1091 浏览 2 评论 0原文

如何使 SKAction.move() 随机且永远重复?

例如:

let boxPosition = Double.random(in: 100…600)
let boxMove = SKAction.move(to: CGPoint(x: boxPosition, y: 100), duration: 10)

let boxRepeat = SKAction.repeatForever(boxMove)
box.run(boxRepeat)

我知道在上面的示例中不会更新值......

我有一个可行的解决方案,但感觉不正确。该序列不起作用...

import SpriteKit

class GameScene: SKScene {

let box = SKSpriteNode()

override func didMove(to view: SKView) {

    box.texture = SKTexture(imageNamed: "box")
    box.size = CGSize(width: 50, height: 50)
    box.position = CGPoint(x: 200, y: 200)
    addChild(box)
    
    let boxRandom = SKAction.run({
        let boxPosition = Double.random(in: 300...500)
        let boxMove = SKAction.move(to: CGPoint(x: 200, y: boxPosition), duration: 2)
        self.box.run(boxMove)
    })
    
    let boxMove = SKAction.move(to: CGPoint(x: 200, y: 100), duration: 2)
    
    let boxGroup = SKAction.sequence([boxMove, boxRandom])
    let boxRepeat = SKAction.repeatForever(boxGroup)
    box.run(boxRepeat)

}
}

有什么建议吗?感谢您提前提供的所有答案。

How can I make SKAction.move() to be random and repeatForever?

For example:

let boxPosition = Double.random(in: 100…600)
let boxMove = SKAction.move(to: CGPoint(x: boxPosition, y: 100), duration: 10)

let boxRepeat = SKAction.repeatForever(boxMove)
box.run(boxRepeat)

I know that in above example will not update the value…

I have solution that works but doesn't feel right. The sequence won't work...

import SpriteKit

class GameScene: SKScene {

let box = SKSpriteNode()

override func didMove(to view: SKView) {

    box.texture = SKTexture(imageNamed: "box")
    box.size = CGSize(width: 50, height: 50)
    box.position = CGPoint(x: 200, y: 200)
    addChild(box)
    
    let boxRandom = SKAction.run({
        let boxPosition = Double.random(in: 300...500)
        let boxMove = SKAction.move(to: CGPoint(x: 200, y: boxPosition), duration: 2)
        self.box.run(boxMove)
    })
    
    let boxMove = SKAction.move(to: CGPoint(x: 200, y: 100), duration: 2)
    
    let boxGroup = SKAction.sequence([boxMove, boxRandom])
    let boxRepeat = SKAction.repeatForever(boxGroup)
    box.run(boxRepeat)

}
}

Any suggestion? Thanks for all the answers in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2025-01-17 15:38:28

SKAction.run 是即时发生的,因此它不会等待其中的 SKAction.move 完成。

对代码的一个简单修复是添加一个 SKAction.wait ,其持续时间与块内的 SKAction.move 相同:

let wait = SKAction.wait(forDuration: 2)

let boxGroup = SKAction.sequence([boxMove, boxRandom, wait])

如果您需要使用为每个移动操作自定义持续时间,您可以创建一个方法来运行随机移动并在移动完成时递归调用相同的方法。像这样的事情:

override func didMove(to view: SKView) {
    recursiveRandomMove()
}

func recursiveRandomMove() {
    let random = CGFloat.random(in: 100...400)
    let duration = Double.random(0...10) // your custom duration here
    box.run(SKAction.sequence([
        SKAction.moveTo(x: random, duration: duration),
        SKAction.run {
            self.recursiveRandomMove()
        }
    ]))
}

还可以考虑查看驱动游戏逻辑以更好地控制您的结果。

The SKAction.run takes place instantaneously, so it won't wait for the completion of the SKAction.move inside it.

A simple fix for your code would be to add an SKAction.wait for the same duration as the SKAction.move inside the block to the sequence:

let wait = SKAction.wait(forDuration: 2)

let boxGroup = SKAction.sequence([boxMove, boxRandom, wait])

If you need to use a custom duration for each move action, you could create a method that will run the random move and recursively call the same method when the move completes. Something like this:

override func didMove(to view: SKView) {
    recursiveRandomMove()
}

func recursiveRandomMove() {
    let random = CGFloat.random(in: 100...400)
    let duration = Double.random(0...10) // your custom duration here
    box.run(SKAction.sequence([
        SKAction.moveTo(x: random, duration: duration),
        SKAction.run {
            self.recursiveRandomMove()
        }
    ]))
}

Also consider looking at Drive Game Logic for more control over your outcomes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文