sprite kit - How to make a node visible before any user interaction -
i took , modified piece of code allows me shoot bullets in every direction. works perfectly. generating random bullet in moment when player touch ended. have 6 different bullets , purposes of game need player see them before tap screen. tried initialize bullet in global function , pass didmovetoview. it's not working, bullet generating once , stays @ place. code initializing bullet:
class gamescene: skscene, skphysicscontactdelegate { var gameover = false let cannon = skspritenode(imagenamed: "cannon") let bulletincannon = skspritenode() var endofscreenright = cgfloat() var endofscreenleft = cgfloat() let bmrarray = ["blbl", "magbl", "rbl"] let cgyarray = ["cbl", "gbl", "ybl"] let yrmarray = ["ybl", "rbl", "magbl"] let bulletarray = ["rbullet","magbullet", "blbullet", "cbullet", "gbullet", "ybullet"] override func didmovetoview(view: skview) { endofscreenleft = (self.size.width / 2) * cgfloat(-1) endofscreenright = self.size.width / 2 cannon.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) self.addchild(cannon) cannon.zposition = 0 var randombullet = int(arc4random_uniform(6)) bulletincannon.texture = sktexture(imagenamed: bulletarray[randombullet]) bulletincannon.position = cannon.position addchild(bulletincannon) bulletincannon.zposition = 1000 runaction(skaction.repeatactionforever(skaction.sequence([skaction.runblock(addcgyline), skaction.waitforduration(1.0)]))) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { } override func touchesended(touches: set<nsobject>, withevent event: uievent) { touch: anyobject in touches { let location = touch.locationinnode(self) let bullet = skspritenode(texture: bulletincannon.texture) bullet.position = cannon.position addchild(bullet) let offset = location - bullet.position let direction = offset.normalized() let shootamount = direction * 1000 let realdestination = shootamount + bullet.position let actionmove = skaction.moveto(realdestination, duration: 2.0) let actiondone = skaction.removefromparent() bullet.runaction(skaction.sequence([actionmove, actiondone])) var randombullet = int(arc4random_uniform(6)) bulletincannon.texture = sktexture(imagenamed: bulletarray[randombullet]) } } func addcgyline () { var cgyblock = skspritenode(imagenamed: "cyanbox") cgyblock.position = cgpointmake(size.width + cgyblock.size.width/2, cgrectgetmidy(self.frame) + 60) addchild(cgyblock) var randomcgy = int(arc4random_uniform(3)) cgyblock.texture = sktexture(imagenamed: cgyarray[randomcgy]) let actionmove = skaction.moveto(cgpoint(x: -cgyblock.size.width/2, y: cgrectgetmidy(self.frame) + 60), duration: 3) let actiondone = skaction.removefromparent() cgyblock.runaction(skaction.sequence([actionmove, actiondone])) skactiontimingmode.easeout }
how can generate bullet , shoot it? glad hear ideas, thanks!
you can generate bullet in cannon global variable, , create new bullet in touchesended
bullet fires cannon. set new bullet's texture global bullet's texture. after firing action, give global bullet new texture. if need have reload time, remove global bullet temporarily, , use boolean
"lock" cannon firing until time has elapsed.
here's code used. if cannon moves, make sure update position of global bullet. subtraction of cgpoint
gives me error, i'm using xcode 6.4.
edit: quicker , easier way giving bulletincannon
value
var bulletincannon = skspritenode(imagenamed: "rbullet") override func didmovetoview(view: skview) { var randombullet = int(arc4random_uniform(6)) bulletincannon.texture = sktexture(imagenamed: bulletarray[randombullet]) bulletincannon.position = cannon.position addchild(bulletincannon) } override func touchesended(touches: set<nsobject>, withevent event: uievent) { touch in (touches as! set<uitouch>) { let location = touch.locationinnode(self) // create bullet fire cannon let bullet = skspritenode(texture: bulletincannon.texture) bullet.position = cannon.position addchild(bullet) let offset = location - bullet.position let direction = offset.normalized() let shootamount = direction * 1000 let realdestination = shootamount + bullet.position let actionmove = skaction.moveto(realdestination, duration: 2.0) let actiondone = skaction.removefromparent() bullet.runaction(skaction.sequence([actionmove, actiondone])) // generate random texture global bullet var randombullet = int(arc4random_uniform(6)) bulletincannon.texture = sktexture(imagenamed: bulletarray[randombullet]) } }
Comments
Post a Comment