ios - don't know how to update the score label node , when all my nodes are declared locally -


i created game ball passes through obstacles fall down in random positions , , when ball passes through obstacle score should increased 1 , nodes of objects declared locally , don't how increase score out creating many nodes.

here example: of 1 of functions:

func leftobject(){         var rand = arc4random_uniform(2) + 1         if rand == 1         {             var bigwall = skspritenode(imagenamed: "shortwall")              bigwall.position = cgpointmake(cgrectgetminx(self.frame) + bigwall.size.width / 2, cgrectgetmaxy(self.frame))              var moveobjects = skaction.movebyx(0, y: -self.frame.size.height * 2, duration: nstimeinterval(self.frame.size.height / 100))             var removeobjects = skaction.removefromparent()             var moveandremoveobjects = skaction.sequence([moveobjects,removeobjects])             bigwall.runaction(moveandremoveobjects)             bigwall.physicsbody = skphysicsbody(rectangleofsize: bigwall.size)             bigwall.physicsbody?.dynamic = false             bigwall.physicsbody?.categorybitmask = objectgroup            }          else         {             var tallwall = skspritenode(imagenamed: "shortwall")             tallwall.position = cgpointmake(cgrectgetminx(self.frame) + tallwall.size.width / 2, cgrectgetmaxy(self.frame))             var moveobjects = skaction.movebyx(0, y: -self.frame.size.height * 2, duration: nstimeinterval(self.frame.size.height / 100))             var removeobjects = skaction.removefromparent()             var moveandremoveobjects = skaction.sequence([moveobjects,removeobjects])             tallwall.runaction(moveandremoveobjects)             tallwall.physicsbody = skphysicsbody(rectangleofsize: tallwall.size)             tallwall.physicsbody?.categorybitmask = objectgroup             tallwall.physicsbody?.dynamic = false                movingobjects.addchild(tallwall)         } 

and have function being called every 1 second generates random number call 6 functions displays objects randomly. if know way if have change code , please help. thank you.

you have few options here, code design decisions.

first, code duplicate should put separate method instead of sitting in if/else. looks of it, first 8 lines or duplicate. make new method:

func setupwall() -> skspritenode {    var tallwall = skspritenode(imagenamed: "shortwall")    //all other lines duplicate; } 

this way call method in both if/else conditions:

var rand = arc4random_uniform(2) + 1  var wall = setupwall()  if rand == 1 {  //any unique stuff } else {  //other unique stuff } 

secondly, able access wall or variable outside of method can declare them instance variables @ top of class

 var wall = skspritenode() //its declared var, can update inside method call , still access outside method 

this 1 way, way have map or dictionary of sort hold local variable in it. dictionary has key value pair, can assign specific key name of variable, , assign right object value

you can read them here, i'll provide example below: https://developer.apple.com/library/mac/documentation//general/reference/swiftstandardlibraryreference/dictionary.html

//declare dictionary instance variable (a class variable, not inside method) //pseudocode, gotta make sure syntax correct , compiles  var mydict = dictionary<string: skspritenode>() 

and in method when create sprites want access outside of method, add them dictionary , give them name know:

var bigwall = skspritenode(imagenamed: "shortwall") mydict["mywall1"] = bigwall  //then later can say: let mywallfromothermethod = mydict["mywall1"]   //but check if key exists first // don't want nil error // use syntax  if let mywallfromothermethod = mydict["mywall1"] { // val not nil , optional has been unwrapped, use } 

like said before, need make sure complies syntax i've added because haven't tested in xcode. also, since don't know how overall project designed, 1 method may work better other.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -