sprite kit - SpriteKit: Having trouble updating text of a SKLabelNode during runtime (2 approaches) -
i want set text of label based on user's tapping on node. i've tried 2 approaches, neither of have worked intended.
approach #1: declare sklabelnode globally, update sklabelnode.text @ runtime. code:
import spritekit class gamescene: skscene { //level 0 var q0label:sklabelnode! var q1label:sklabelnode! let gem = skspritenode(imagenamed:"destiny_gem") let energy = skspritenode(imagenamed: "destiny_energy") let money = skspritenode(imagenamed: "destiny_money") override func didmovetoview(view: skview) { //level 0 - choose destiny //add label scene q0label = sklabelnode(fontnamed: "courier") q0label.text = "why want doctor?" q0label.fontsize = 30 q0label.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) self.addchild(q0label) q1label = sklabelnode(fontnamed: "courier") q1label.fontsize = 25 q1label.position = cgpoint(x:frame.size.width * 0.5, y: frame.size.height * 0.3) //position sprites gem.position = cgpoint(x:frame.size.width * 0.2, y: frame.size.height * 0.4) gem.zposition = 2 [gem.setscale(0.5)] gem.name = "gem" energy.position = cgpoint(x:frame.size.width * 0.5, y: frame.size.height * 0.4) energy.zposition = 2 [energy.setscale(0.5)] energy.name = "energy" money.position = cgpoint(x:frame.size.width * 0.8, y: frame.size.height * 0.4) money.zposition = 2 [money.setscale(0.25)] money.name = "money" //add sprites scene addchild(gem) addchild(energy) addchild(money) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { /* called when touch begins */ touch: anyobject in touches { let location = (touch as! uitouch).locationinnode(self) if let thename = self.nodeatpoint(location).name { if thename == "gem" { q1label.text = "like parents before me, destiny." self.addchild(q1label) } else if thename == "energy" { q1label.text = "the people of world need help." self.addchild(q1label) } else if thename == "money" { q1label.text = "i want rich , famous." self.addchild(q1label) } } } } }
problem: work first time click node, next click, application crash, error message: "attemped add sknode has parent error"
i read various solutions on not declaring object globally. lead approach #2.
approach #2: declare label conditionally. code:
override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { /* called when touch begins */ touch: anyobject in touches { let location = (touch as! uitouch).locationinnode(self) if let thename = self.nodeatpoint(location).name { if thename == "gem" { var q1label:sklabelnode! q1label = sklabelnode(fontnamed: "courier") q1label.fontsize = 25 q1label.position = cgpoint(x:frame.size.width * 0.5, y: frame.size.height * 0.3) q1label.text = "like parents before me, destiny." //set destiny variable = gem self.addchild(q1label) } else if thename == "energy" { var q1label:sklabelnode! q1label = sklabelnode(fontnamed: "courier") q1label.fontsize = 25 q1label.position = cgpoint(x:frame.size.width * 0.5, y: frame.size.height * 0.3) q1label.text = "the people of world need help." //set destiny variable = energy self.addchild(q1label) } else if thename == "money" { var q1label:sklabelnode! q1label = sklabelnode(fontnamed: "courier") q1label.fontsize = 25 q1label.position = cgpoint(x:frame.size.width * 0.5, y: frame.size.height * 0.3) q1label.text = "i want rich , famous." //set destiny variable = money self.addchild(q1label) } } } } }
problem: creates labels on top of each other; don't know how clean created earlier.
re-creating labels in case unnecessary. want update label based on button clicked. thing need create 2 labels once in didmovetoview method , update them accordingly, (just copy/past code try):
import spritekit class gamescene: skscene { //level 0 var q0label:sklabelnode! var q1label:sklabelnode! let gem = skspritenode(imagenamed:"destiny_gem") let energy = skspritenode(imagenamed: "destiny_energy") let money = skspritenode(imagenamed: "destiny_money") override func didmovetoview(view: skview) { //level 0 - choose destiny //add label scene q0label = sklabelnode(fontnamed: "courier") q0label.text = "why want doctor?" q0label.fontsize = 30 q0label.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) self.addchild(q0label) q1label = sklabelnode(fontnamed: "courier") q1label.fontsize = 25 q1label.position = cgpoint(x:frame.size.width * 0.5, y: frame.size.height * 0.3) q1label.text = "" self.addchild(q1label) //position sprites gem.position = cgpoint(x:frame.size.width * 0.35, y: frame.size.height * 0.4) gem.zposition = 2 [gem.setscale(0.5)] gem.name = "gem" energy.position = cgpoint(x:frame.size.width * 0.3, y: frame.size.height * 0.4) energy.zposition = 2 [energy.setscale(0.5)] energy.name = "energy" money.position = cgpoint(x:frame.size.width * 0.4, y: frame.size.height * 0.4) money.zposition = 2 [money.setscale(0.25)] money.name = "money" //add sprites scene addchild(gem) addchild(energy) addchild(money) } override func touchesbegan(touches: nsset, withevent event: uievent) { /* called when touch begins */ touch: anyobject in touches { let location = (touch uitouch).locationinnode(self) if let thename = self.nodeatpoint(location).name { if thename == "gem" { q1label.text = "like parents before me, destiny." } else if thename == "energy" { q1label.text = "the people of world need help." } else if thename == "money" { q1label.text = "i want rich , famous." } } } } }
just don't re-add labels multiple times. , error getting...keep in mind node can have 1 parent @ time, , that's why getting error saying label has parent.
Comments
Post a Comment