ios - My Sprite Kit button won't work in Swift -
ios - My Sprite Kit button won't work in Swift -
hi looking @ wondering why code in swift sprite kit won't work can run when click on button not work(it not go scene) when add together println if statement , click it run println. give thanks in advance!
here code first scene:
// // gamescene.swift // pocket rocket // // created lucas farleigh on 11/11/2014. // copyright (c) 2014 lucas farleigh. rights reserved. // import spritekit; class gamescene: skscene { allow background = skspritenode(imagenamed:"background") allow playbutton = skspritenode(imagenamed: "playbutton") override func didmovetoview(view: skview) { playbutton.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidx(self.frame)) background.yscale = 2 background.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidx(self.frame)) self.addchild(background) self.addchild(playbutton) } override func touchesbegan(touches: nsset, withevent event: uievent) { /* called when touch begins */ //making scene vars var scene = playscene(size: self.size) touch: anyobject in touches { allow location = touch.locationinnode(self) allow node = self.nodeatpoint(location) allow skview = self.view skview! scene.size = skview.bounds.size playbutton.name = "pb" if node.name == "pb"{ println("it worked") skview.presentscene(scene) } } } override func update(currenttime: cftimeinterval) { /* called before each frame rendered */ } }
and sec scene(playscene)
// // playscene.swift // pocket rocket // // created lucas farleigh on 14/11/2014. // copyright (c) 2014 lucas farleigh. rights reserved. // import spritekit import uikit import foundation class playscene:skscene{ allow background = skspritenode(imagenamed: "background") allow bar1 = skspritenode(imagenamed:"bar1") allow bar2 = skspritenode(imagenamed:"bar2") allow bar3 = skspritenode(imagenamed:"bar3") allow bar4 = skspritenode(imagenamed:"bar4") allow bar5 = skspritenode(imagenamed:"bar5") allow bar6 = skspritenode(imagenamed:"bar6") override func didmovetoview(view: skview) { background.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidx(self.frame)) addchild(background) background.yscale = 2
note creating background
, playbutton
nodes twice: 1 time class property, , sec time local variable in didmovetoview
. local version adding scene kid view. if want able test against these nodes in other methods in class, rid of local declarations in didmovetoview
. can still fill in other info these nodes, remove let
statements.
then in touchesbegan
want location of touch in scene passing self
touch.locationinnode(self)
, retrieve node corresponds touch:
override func touchesbegan(touches: nsset, withevent event: uievent) { touch: anyobject in touches { allow location = touch.locationinnode(self) allow node = self.nodeatpoint(location) if node == play_button { println("hello!!!!") } } }
a sec way accomplish playbutton
give node name when create it:
play_button.name = "playbutton"
and touchesbegan
this:
override func touchesbegan(touches: nsset, withevent event: uievent) { touch: anyobject in touches { allow location = touch.locationinnode(self) allow node = self.nodeatpoint(location) if node.name == "playbutton" { println("hello!!!!") } } }
ios swift xcode6 osx-yosemite
Comments
Post a Comment