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

查看:108
本文介绍了移动时修改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 课程中,你有一个更新(deltaTime秒: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天全站免登陆