Modify property on SKNode while moving

匆匆过客 提交于 2019-12-01 13:48:35

In your GameScene.swift class you have an update(deltaTime seconds: TimeInterval) function that can track one second intervals. Add a class level variable to hold the accumulated time, then check every one second if your creature is running its action.

class GameScene : SKScene {
    private var accumulatedTime: TimeInterval = 0

    override func update(_ currentTime: TimeInterval) {    
        if (self.accumulatedTime == 0) {
            self.accumulatedTime = currentTime
        }

        if currentTime - self.accumulatedTime > 1 {
            if creatureNode.action(forKey: "moveActionKey") != nil {
               // TODO: Update energy status
            }

            // Reset counter
            self.accumulatedTime = currentTime
        }
    }
}

If you know the energy property, then just use that as your duration, and use the move(by: SKAction.

If you want your energy to deplete with it, use an SKAction.customAction in a group to decreate it

var previousTime = 0
let move = SKAction.moveBy(x:dx * energy,y:dy * energy,duration:energy)
let depreciateEnergy = SKAction.customAction(withDuration:energy,{(node,time) in node.energy -= (time - previuosTime);previousTime = time}) 
let group = [move,depreciateEnergy]
character.run(group,withKey:"character")

Now if at any time you need to stop the action, just call character.removeActionForKey("character"), and your energy meter will stay at whatever energy is remaining.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!