ios - touch moking sprite by tap not swipe swift -
i have block in code sprite move if tap screen. have behaviour when swipe or down instead. here code
import spritekit import uikit class gamescene: skscene { var porker:porker! var touchlocation = cgfloat() var gameover = false override func didmovetoview(view: skview) { addbg() addpig() } func addbg() { let bg = skspritenode(imagenamed: "bg"); addchild(bg) } func addpig() { let pig = skspritenode(imagenamed: "pig") porker = porker(guy:pig) addchild(pig) } override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { touch: anyobject in touches{ if !gameover { touchlocation = (touch.locationinview(self.view!).y * -1) + (self.size.height/2) } } let moveaction = skaction.movetoy(touchlocation, duration: 0.5) moveaction.timingmode = skactiontimingmode.easeout porker.guy.runaction(moveaction) } } func update(currenttime: cftimeinterval) { /* called before each frame rendered */ }
replace touchesbegan code:
// store start touch position let ytouchcurrentposition = 0.0 let ytouchdistance = 0.0 let ytouchstartposition = 0.0 override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { touch in touches { ytouchstartposition = touch.locationinnode(self).y } } // calculate distance of touch movement override func touchesmoved(touches: set<uitouch>, withevent event: uievent?) { touch in touches { ytouchcurrentposition = touch.locationinnode(self).y ytouchdistance = ytouchstartposition - ytouchcurrentposition } } // reset movement states , move sprite override func touchesended(touches: set<uitouch>, withevent event: uievent?) { ytouchcurrentposition = 0.0 ytouchdistance = 0.0 ytouchstartposition = 0.0 let moveaction = skaction.movetoy(cgpoint(porker.guy.location.x, porker.guy.location.y + ytouchdistance), duration: 0.5) moveaction.timingmode = skactiontimingmode.easeout porker.guy.runaction(moveaction) }
Comments
Post a Comment