Reputation: 37
I have this code below and I just want to make a simple addition game that prints out the money when you tap. I have it set to print out the currentMoney variable which is 0 + 5 and so on but I can not get the label to change with it.
I tried setting myLabel.text = String(currentMoney)
in the touches began but it will not update myLabel
, how can I achieve this?
import SpriteKit
var currentMoney = 0
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = String(currentMoney)
myLabel.fontSize = 45
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
currentMoney = currentMoney + 5
print(currentMoney)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Upvotes: 2
Views: 2369
Reputation: 205
class GameScene: SKScene {let myLabel = SKLabelNode(fontNamed:"Chalkduster") var currentMoney = 0 override func didMoveToView(view: SKView) { /* Setup your scene here */ /* Setup your scene here */ myLabel.text = String(currentMoney) myLabel.fontSize = 45 myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame)) self.addChild(myLabel) } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { /* Called when a touch begins */ currentMoney = currentMoney + 5 print(currentMoney) myLabel.text = String(currentMoney) } override func update(currentTime: CFTimeInterval) { /* Called before each frame is rendered */ }
}
Upvotes: 0
Reputation: 1213
You can't change the text because you can't access you label outside the moveToView function. Try this:
import SpriteKit
var currentMoney = 0
var myLabel: SKLabelNode! //made it globally
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = String(currentMoney)
myLabel.fontSize = 45
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
currentMoney = currentMoney + 5
myLabel.text = String(currentMoney)
print(currentMoney)
}
Upvotes: 1
Reputation: 13675
You can observe the change of currentMoney
variable and take appropriate actions (update label's text property). To do this, you can make a label as a property of your scene, and use didSet
property observer like this:
import SpriteKit
class GameScene: SKScene {
var currentMoney = 0 {
didSet{
myLabel.text = String(currentMoney)
}
}
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
myLabel.text = String(currentMoney)
myLabel.fontSize = 45
myLabel.position = CGPoint(x:frame.midX, y: frame.midY)
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
currentMoney = currentMoney + 5
}
}
Upvotes: 0