【问题标题】:Varying value inside runAction() not changingrunAction() 中的变化值不变
【发布时间】:2015-11-23 05:28:52
【问题描述】:

我有一个运行动作循环,它产生一个敌人,然后等待并产生另一个。目的是分数越高,敌人生成的速度越快。

但是无论如何,当前的代码都以相同的速度生成敌人。我一直在 override func update(currentTime: NSTimeInterval) 方法中更新速率,所以我不知道是什么问题。

override func update(currentTime: NSTimeInterval){
spawnRate = 2.0-(0.1*(Double)(((Double)(Score))/(10.0)))
    if(spawnRate < 0.5){
        spawnRate = 0.5
    }
}
override func didMoveToView(view: SKView) {
    runAction(SKAction.repeatActionForever(
            SKAction.sequence([
                SKAction.runBlock(addEnemy),
                SKAction.waitForDuration(spawnRate)
                ])
            ))
}

【问题讨论】:

  • 除非您显示一些代码,否则我们无法真正帮助您。
  • 您需要向他人展示您的尝试,以便更好地了解如何帮助您。
  • 还有一些代码,我在游戏中更改了生成速度,它们以我设置的初始速度生成。这就是我所尝试的。

标签: swift sprite-kit


【解决方案1】:

目前正在发生的事情是你已经:

  • 在动作中存储的持续时间参数
  • 在序列中一遍又一遍地重复使用该动作

所以实际上没有任何变化。为了解决这个问题,一种方法是使用递归调用并每次更改持续时间参数:

import SpriteKit

class GameScene: SKScene {

    let  shape = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 20, height: 20))

    var delay:NSTimeInterval = 1.0

    override func didMoveToView(view: SKView) {

       recursiveMethod()

    }

    func recursiveMethod(){



        let recursive = SKAction.sequence([

            SKAction.waitForDuration(delay),
            SKAction.runBlock({self.recursiveMethod();println("delay \(self.delay)")})
         ])

        runAction(recursive)

    }

    override func update(currentTime: NSTimeInterval) {
        if(delay > 0.2){delay = delay - 0.001}
    }

}

【讨论】:

    【解决方案2】:

    问题在于:

    SKAction.sequence([
         SKAction.runBlock(addEnemy),
         SKAction.waitForDuration(spawnRate)
    ])
    

    被创造出来,然后永远重复。

    试着把代码放在一个

    runAction(SKAction.repeatActionForever(runblock(spawnAndWait()))
    

    然后做一个单独的函数。

    spawnAndWait() { // put here the code for spawning the enemy and waiting spawnrate duration.
    

    b.t.w
    将更新 spawnRate 的代码放在分数增加的地方。因为这样更有效率。然后在更新方法中,因为它不断被调用。

    【讨论】:

    • 但是没有注册等待是没有用的。它只是无限地产生了它们。
    • 你能支持我的回答吗?只是这样我以后可以发表评论
    • 我的声望不够
    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2021-12-05
    • 2022-11-02
    • 1970-01-01
    相关资源
    最近更新 更多