swift - Accessing properties of multiple SKShapeNodes -


in program have method called addobstacle, creates rectangular skshapenode skphysicsbody, , leftward velocity.

func addobstacle(bottom: cgfloat, top: cgfloat, width: cgfloat){     let obstaclerect = cgrectmake(self.size.width + 100, bottom, width, (top - bottom))     let obstacle = skshapenode(rect: obstaclerect)     obstacle.name = "obstaclenode"     obstacle.fillcolor = uicolor.graycolor()     obstacle.physicsbody = skphysicsbody(edgeloopfrompath: obstacle.path!)     obstacle.physicsbody?.dynamic = false     obstacle.physicsbody?.affectedbygravity = false     obstacle.physicsbody?.contacttestbitmask = physicscatagory.ball     obstacle.physicsbody?.categorybitmask = physicscatagory.obstacle     obstacle.physicsbody?.usesprecisecollisiondetection = true        self.addchild(obstacle)     obstacle.runaction(skaction.moveby(obstaclevector, duration: obstaclespeed)) } 

in separate method, called endgame, want fade out obstacles in existence on screen. obstacle objects private, makes accessing properties difficult. if there 1 on screen, can access name. however, when childnodewithname("obstaclenode")?.runaction(skaction.fadealphaby(-1.0, duration: 1.0)), 1 of "obstacles" fades away; rest remain opaque. there way of doing this? in advance (:

you go with:

self.enumeratechildnodeswithname("obstaclenode", usingblock: {     node, stop in      //do stuff }) 

more method can found here.

in example assumed you've added obstacles scene. if not, instead of scene, run method on obstacle's parent node.

and 1 side note...skshapenode not performant solution in many cases because requires @ least 1 draw pass rendered scene (it can't drawn in batches skspritenode). if using skshapenode not "a must" in app, , can switch them skspritenode, warmly suggest because of performance.

spritekit can render hundreds of nodes in single draw pass if using same atlas , same blending mode sprites. not case skshapenodes. more here. search topic, there useful posts this.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -