我如何获得资金Int由SKAction运行的功能更新?

时间:2015-11-07 16:37:40

标签: swift sprite-kit skaction

因此,我试图获得金钱标签将其文本更新为金钱:Int的价值正在增加' x'每秒数量。这是我的代码,我不知道这是错误的还是需要的:

    var money: Int = 0

    let moneyLabel = SKLabelNode(fontNamed:"Futura")
    moneyLabel.position = CGPoint(x: CGRectGetMidX(self.frame),y: CGRectGetMidY(self.frame))
    moneyLabel.fontColor = SKColor.greenColor()
    moneyLabel.fontSize = 24
    moneyLabel.text = "\(money)"

    self.addChild(moneyLabel)



    func MoneyTimer() {

        var money = money + 10


    }

    let delay = SKAction.waitForDuration(1)
    let dayTimer = SKAction.sequence([MoneyTimer(), delay])

我使用swift和SpriteKit

1 个答案:

答案 0 :(得分:1)

SKAction.runBlockSKAction.repeatActionForever将帮助您实现目标。

用以下代码替换您的代码:

var money: Int = 0

let moneyLabel = SKLabelNode(fontNamed:"Futura")
moneyLabel.position = CGPoint(x: CGRectGetMidX(self.frame),y: CGRectGetMidY(self.frame))
moneyLabel.fontColor = SKColor.greenColor()
moneyLabel.fontSize = 24
moneyLabel.text = "\(money)"

self.addChild(moneyLabel)

let moneyTimer = SKAction.runBlock {
    money += 10
    moneyLabel.text = "\(money)"
}

let delay = SKAction.waitForDuration(1)
let dayTimer = SKAction.sequence([moneyTimer, delay])
self.runAction(SKAction.repeatActionForever(dayTimer))

另一方面,我会将变量货币更改为当前类别的财产。