我有一个 SKNode 的子类,它充当我的“生物”。它们使用 SKActions 自动在场景中移动。我有兴趣在生物移动时修改(减少)“能量”属性(Int)。
不保证生物能够移动 SKAction 的整个长度(它可以被中断),因此计算总距离然后在它开始移动时立即减少属性并不理想。我本质上想说的是“节点每移动 1 秒,能量属性就会减少”。
我怎样才能做到这一点?我不知所措了!谢谢。
In your GameScene.swift
类你有一个update(deltaTime seconds: TimeInterval)
可以跟踪一秒间隔的功能。添加一个类级别变量来保存累积时间,然后每秒检查您的生物是否正在执行其操作。
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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)