移动时修改 SKNode 上的属性 [英] Modify property on SKNode while moving

查看:15
本文介绍了移动时修改 SKNode 上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SKNode 的子类,它充当我的生物".这些使用 SKActions 自动在场景中移动.我有兴趣在生物移动时修改(减少)能量"属性(Int).

I have a subclass of SKNode that acts as my "creature". These move about the scene automatically using SKActions. I'm interested in modifying (decreasing) an 'energy' property (Int) as the creature moves.

不能保证该生物移动整个移动 SKAction 长度(它可以被打断),因此计算总距离然后在它开始移动时立即减少属性并不理想.我基本上想说节点每移动 1 秒,降低能量属性".

The creature isn't guaranteed to move the entire length of the move SKAction (it can be interrupted), so calculating the total distance and then decreasing the property as soon as it starts moving isn't ideal. I'd essentially like to say "for every 1 second the node is moving, decrease the energy property".

我该怎么做?我不知所措!谢谢.

How can I do this? I'm at a loss! Thank you.

推荐答案

在你的 GameScene.swift 类中你有一个 update(deltaTime seconds: TimeInterval) 函数,它可以跟踪一秒间隔.添加一个类级别的变量来保存累计时间,然后每隔一秒检查一次你的生物是否正在运行它的动作.

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
        }
    }
}

这篇关于移动时修改 SKNode 上的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆