一定的时间后停止滑雪

发布于 2025-01-19 09:51:17 字数 1826 浏览 2 评论 0原文

我有许多敌人在游戏中的不同时间生成,并使用 SKAction 移动,但在每个关卡结束时我都会生成一个 Boss。当 Boss 生成时,我希望其他敌人停止生成。我对每个敌人都有一个函数。我在 SKAction.sequence 中为每个敌人使用 withKey:,然后在 boss 函数中使用 removeAction(forKey: )停止它,但它不起作用。我是否以错误的方式使用它。

我的代码示例(仅保留在 SKAction 代码中,删除了所有其他代码)

override func didMove(to view: SKView) {
       //Spawn Enemy1
        run(SKAction.repeatForever(
          SKAction.sequence([SKAction.run() { [weak self] in
                              self?.addEnemy1()
                            },
                            SKAction.wait(forDuration: 5.0)])))

}

func addEnemy1() {
        
        let delay = SKAction.wait(forDuration: 10)
        let actionMove = SKAction.moveTo(y: -600, duration: 5.0)
        let actionRemove = SKAction.removeFromParent()
        enemy1.run(SKAction.sequence([delay, actionMove, actionRemove]), withKey:"enemy1")
        
    }

func addBoss() {
        
        let move1 = SKAction.moveTo(y: size.height / 3.5, duration: 3)
        let move2 = SKAction.moveTo(x: size.width / 3, duration: 3)
        let move3 = SKAction.moveTo(x: 0 - boss.size.width, duration: 3)
        let move4 = SKAction.moveTo(x: 0, duration: 1.5)
        let move5 = SKAction.fadeOut(withDuration: 0.2)
        let move6 = SKAction.fadeIn(withDuration: 0.2)
        let move7 = SKAction.moveTo(y: 0 - boss.size.height * 3, duration: 3)
        let move8 = SKAction.moveTo(y: size.height / 3.5, duration: 3)

        let action = SKAction.repeat(SKAction.sequence([move5, move6]), count: 6)
        let repeatForever = SKAction.repeatForever(SKAction.sequence([move2, move3, move4, action, move7, move8]))
        let sequence = SKAction.sequence([move1, repeatForever])

        boss.run(sequence)
        
        enemy1.removeAction(forKey: "enemy1")
    }

I have a number of enemies that spawn at separate times in my game, and move using SKAction but at the end of each level I have a boss that spawns in. When the boss spawns I want the other enemies to stop spawning. I have a function for each enemy. I'm using the withKey: in my SKAction.sequence for each enemy and then use the removeAction(forKey: ) in the boss function to stop it but it doesn't work. Am I using this in a wrong way.

example of my code (only left in the SKAction code, removed all other code)

override func didMove(to view: SKView) {
       //Spawn Enemy1
        run(SKAction.repeatForever(
          SKAction.sequence([SKAction.run() { [weak self] in
                              self?.addEnemy1()
                            },
                            SKAction.wait(forDuration: 5.0)])))

}

func addEnemy1() {
        
        let delay = SKAction.wait(forDuration: 10)
        let actionMove = SKAction.moveTo(y: -600, duration: 5.0)
        let actionRemove = SKAction.removeFromParent()
        enemy1.run(SKAction.sequence([delay, actionMove, actionRemove]), withKey:"enemy1")
        
    }

func addBoss() {
        
        let move1 = SKAction.moveTo(y: size.height / 3.5, duration: 3)
        let move2 = SKAction.moveTo(x: size.width / 3, duration: 3)
        let move3 = SKAction.moveTo(x: 0 - boss.size.width, duration: 3)
        let move4 = SKAction.moveTo(x: 0, duration: 1.5)
        let move5 = SKAction.fadeOut(withDuration: 0.2)
        let move6 = SKAction.fadeIn(withDuration: 0.2)
        let move7 = SKAction.moveTo(y: 0 - boss.size.height * 3, duration: 3)
        let move8 = SKAction.moveTo(y: size.height / 3.5, duration: 3)

        let action = SKAction.repeat(SKAction.sequence([move5, move6]), count: 6)
        let repeatForever = SKAction.repeatForever(SKAction.sequence([move2, move3, move4, action, move7, move8]))
        let sequence = SKAction.sequence([move1, repeatForever])

        boss.run(sequence)
        
        enemy1.removeAction(forKey: "enemy1")
    }

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

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

发布评论

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

评论(1

西瑶 2025-01-26 09:51:17

您正在删除错误的操作。键“enemy1”的动作是:

SKAction.sequence([delay, actionMove, actionRemove])

它只是激活敌人。删除它只会暂时停止这一系列操作。当再次调用 addEnemy1 时,将再次添加操作序列。

生成敌人的操作是您在 self 上运行的操作:

    run(SKAction.repeatForever(
      SKAction.sequence([SKAction.run() { [weak self] in
                          self?.addEnemy1()
                        },
                        SKAction.wait(forDuration: 5.0)])))

您应该为该操作提供一个密钥:

...
},
SKAction.wait(forDuration: 5.0)])), withKey: "spawnEnemy")

然后将其从 self 中删除:

removeAction(forKey: "spawnEnemy")

You are removing the wrong action. The action with key "enemy1" is:

SKAction.sequence([delay, actionMove, actionRemove])

which simply animates the enemy. Removing this only stops this sequence of actions temporarily. When addEnemy1 is called again, the sequence of action is added again.

The action that spawns the enemy is the action that you run on self here:

    run(SKAction.repeatForever(
      SKAction.sequence([SKAction.run() { [weak self] in
                          self?.addEnemy1()
                        },
                        SKAction.wait(forDuration: 5.0)])))

Yo should give a key to that action instead:

...
},
SKAction.wait(forDuration: 5.0)])), withKey: "spawnEnemy")

and then remove that from self:

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