Wednesday, 15 July 2015

sprite kit - Swift SKAction moveTo bottom of screen -



sprite kit - Swift SKAction moveTo bottom of screen -

i trying create 2 sprite nodes fall top of screen bottom of screen, move starting position , fall again, @ moment when reach 3/4 of way downwards screen, 'teleported' screen , there go on fall before beingness 'teleported' little more, fall off bottom of screen expected. here have:

override func didmovetoview(view: skview) { bounds = uiscreen.mainscreen().bounds height = bounds.size.height initfruit() falltimer = nstimer.scheduledtimerwithtimeinterval(0.2, target: self, selector: "fall_timer", userinfo: nil, repeats: true) } func initfruit(){ fruit1 = skspritenode(imagenamed: "fruit_apple") fruit2 = skspritenode(imagenamed: "fruit_banana") fruit1.xscale = 2.5 fruit1.yscale = 2.5 fruit2.xscale = 2.5 fruit2.yscale = 2.5 //position fruit off top of screen, @ separate x coordinates fruit1.position = cgpoint(x: (size.width / 3), y: height + fruit1.frame.height) fruit2.position = cgpoint(x: (size.width / 3) * 2, y: height + fruit1.frame.height) addchild(fruit1) addchild(fruit2) } func fall_timer() { var fallduration: nstimeinterval var moveactiondown: skaction //if fruit @ bottom, move them top if fruit1.position.y == -100 { fruit1.position.y = cgfloat(height + fruit1.frame.height) fruit2.position.y = cgfloat(height + fruit1.frame.height) } if fruit3.position.y == -100 { fruit3.position.y = cgfloat(height + fruit1.frame.height) fruit4.position.y = cgfloat(height + fruit1.frame.height) } //check if fruit @ top, ready fall 1 time again if fruit1.position.y == height + fruit1.frame.height { fallduration = nstimeinterval(arc4random_uniform(6) + 2) moveactiondown = skaction.movetoy(-100.0, duration: fallduration) fruit1.runaction(moveactiondown) } if fruit2.position.y == height + fruit1.frame.height { fallduration = nstimeinterval(arc4random_uniform(6) + 2) moveactiondown = skaction.movetoy(-100.0, duration: fallduration) fruit2.runaction(moveactiondown) } }

i cannot life of me figure why happens. on first fall. after first fall, fruit fall , move top expected.

could run endless action on fruit

let fall=skaction.repeatactionforever(skaction.sequence([ skaction.movetoy(height, duration: 0), skaction.movetoy(0, duration: 1)]))

and perhaps run block when reach bottom

let fall=skaction.repeatactionforever(skaction.sequence([ skaction.movetoy(height, duration: 0), skaction.movetoy(0, duration: 1), skaction.runblock({ () -> void in println("reached bottom") })]))

swift sprite-kit skaction

No comments:

Post a Comment