【问题标题】:Updating parameter of an SKAction that is repeating forever swift快速更新永远重复的 SKAction 的参数
【发布时间】:2015-06-21 10:46:04
【问题描述】:

我现在正在设计的游戏中有一个可以为球设置动画的函数。但是,我希望动画速度随着球的实际速度而变化,我已经实现了这一点,但只有在动画的每次迭代之后才能实现。这使得它有点不稳定,我正在寻找一个更优雅的解决方案,可以在操作中更新 timePerFrame 参数。我最初将其设置为 repeatforever 但意识到 timePerFrame 一旦动作开始就不会更新。有没有办法让这个动画速度变化更流畅?

func animBall() {
    //This is the general runAction method to make our ball change color
    var animSpeedNow = self.updateAnimSpeed()
    println(animSpeedNow)
    var animBallAction = SKAction.animateWithTextures(ballColorFrames, timePerFrame: animSpeedNow,resize: false, restore: true)

    self.runAction(animBallAction, completion: {() -> Void in
        self.animBall()

    })
}

func updateAnimSpeed() -> NSTimeInterval{
    // Update the animation speed based on the velocity to syncrhonize animation with ball velocity
    var velocX = self.physicsBody!.velocity.dx
    var velocY = self.physicsBody!.velocity.dy

    if abs(velocX) > 0 || abs(velocY) > 0 {
        var veloc = sqrt(velocX*velocX + velocY*velocY)
        var animSpeedNow: NSTimeInterval = NSTimeInterval(35/veloc)
        let minAS = NSTimeInterval(0.017)
        let maxAS = NSTimeInterval(0.190)

        if animSpeedNow < minAS {
            return maxAS
        }
        else if animSpeedNow > maxAS {
            return minAS
        }
        else {
            return animSpeedNow
        }

    }
    else {
        return NSTimeInterval(0.15)
    }
}

如果无法直接操作永远运行的 SKAction 的参数,我想我可以

【问题讨论】:

  • 不要试图减慢动画本身的速度,而是通过你的动画实现来控制速度。
  • 作为 Wraithseeker 答案的补充,看看这个stackoverflow.com/q/30853360/3402095也许你会从这些答案中找到有用的东西。
  • Kametrixom:你能说得更具体些吗?有没有一种不同的动画方式对我想要做的事情有用? Whirlwind:我在代码中所做的与该页面上接受的答案基本相同,即使用递归解决方案(self.animball)

标签: ios swift sprite-kit


【解决方案1】:

在您的情况下,一种更简单的方法是删除旧动画动作并用新动作替换它。

【讨论】:

    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多