xcode - Do I have to delete a sprite node once I end up using it? -
i'm creating series of moving pipes in scene. crashes after ~30 pipes generated. because of many nodes in scene , no memories new ones? code this:
import spritekit class gamescene: skscene { var mainpipe: skspritenode = skspritenode() var space:float = 1000 var pipecount:int = 0 override func didmovetoview(view: skview) { self.backgroundcolor = skcolor.blackcolor() self.size.width = 640 self.size.height = 1136 } func randomoffset() -> float{ var rnum:float = float(arc4random()%181) // 0-180 homecoming rnum } var durations: cfloat = 5.0 var colorpipes:uicolor = uicolor.graycolor() func spawnpiperow(offs:float){ self.pipecount = self.pipecount + 1 println("\(self.pipecount)") //offs random number //let offset = offs + (space/2) - 105 allow offset = offs + float(self.size.height/100) - 180 // mainpipe = skspritenode(color:colorpipes, size:cgsize(width: view.bounds.size.width/3, height:700)) mainpipe = skspritenode(color:colorpipes, size:cgsize(width: self.size.width/5, height:self.size.height/1.5)) allow pipebottom = (mainpipe skspritenode).copy() skspritenode allow pipetop = (mainpipe skspritenode).copy() skspritenode allow xx = self.size.width * 2.0 self.setpositionrelativebot(pipebottom, x:float(xx), y: offset ) self.setpositionrelativetop(pipetop, x:float(xx), y: offset + space) pipebottom.physicsbody = skphysicsbody(rectangleofsize: pipebottom.size) pipetop.physicsbody = skphysicsbody(rectangleofsize: pipetop.size) pipebottom.physicsbody?.dynamic = false pipetop.physicsbody?.dynamic = false //pipetop.physicsbody?.contacttestbitmask = birdcategory //pipebottom.physicsbody?.contacttestbitmask = birdcategory self.addchild(pipebottom) self.addchild(pipetop) var actionarray1:nsmutablearray = nsmutablearray() actionarray1.addobject(skaction.moveto(cgpointmake(-1000, pipebottom.size.height - 200), duration: nstimeinterval(durations))) var actionarray2:nsmutablearray = nsmutablearray() actionarray2.addobject(skaction.moveto(cgpointmake(-1000, pipetop.size.height - 200), duration: nstimeinterval(durations))) actionarray1.addobject(skaction.removefromparent()) actionarray2.addobject(skaction.removefromparent()) pipebottom.runaction(skaction.sequence(actionarray1)) pipetop.runaction(skaction.sequence(actionarray2)) } override func touchesbegan(touches: nsset, withevent event: uievent) { /* called when touch begins */ touch: anyobject in touches { } } override func update(currenttime: cftimeinterval) { var timesincelastupdate = currenttime - lastupdatetimerinterval lastupdatetimerinterval = currenttime if(timesincelastupdate > 1){ timesincelastupdate = 1/60 lastupdatetimerinterval=currenttime } updatewithtimesincelastupdate(timesincelastupdate) /* called before each frame rendered */ } func setpositionrelativebot(node:skspritenode, x: float, y: float){ allow xx = (float(node.size.width)/2) + x allow yy = (float(self.size.height)/2) - (float(node.size.height)/2) + y node.position.x = cgfloat(xx) node.position.y = cgfloat(yy) } func setpositionrelativetop(node:skspritenode, x:float, y:float){ allow xx = (float(node.size.width)/2) + x allow yy = (float(self.size.height)/2) + (float(node.size.height)/2) + y node.position.x = cgfloat(xx) node.position.y = cgfloat(yy) } var lastupdatetimerinterval:nstimeinterval = nstimeinterval() var lastyieldtimeinterval:nstimeinterval = nstimeinterval() var speedofbird: cdouble = 1.8 func updatewithtimesincelastupdate(timesincelastupdate:cftimeinterval){ lastyieldtimeinterval += timesincelastupdate if(lastyieldtimeinterval > speedofbird ){ lastyieldtimeinterval=0 self.spawnpiperow(self.randomoffset()) if speedofbird > 0.8{ speedofbird -= 0.1} } } }
try delete them code :
override func update(currenttime: cftimeinterval) { self.enumeratechildnodeswithname("nodename") { node, stop in if (node skspritenode) { allow sprite = node skspritenode // check if node not in scene if (sprite.position.x < -sprite.size.width/2.0 || sprite.position.x > self.size.width+sprite.size.width/2.0 || sprite.position.y < -sprite.size.height/2.0 || sprite.position.y > self.size.height+sprite.size.height/2.0) { sprite.removefromparent() println("outside") } } } }
don't forget named node :
node.name = "nodename"
hope crash stop
xcode swift xcode6
No comments:
Post a Comment