Accesing local defined variable in other functions - Swift -


i making game kind of space invaders. have function creates invaders @ random position.x.

inside addinvader(), have defined attacker skspritenode(imagenamed: "someimage").

this have defined inside function, since there added new attacker every second.

my problem inside function:

override func update(currenttime: nstimeinterval) {     if attacker.position.y < cgfloat(size.height/5*2)         attacker.texture = sktexture(imagenamed: "someotherimage")     } } 

since attacker constant local, update(currenttime: nstimeinterval) function can`t access it.

how can access attacker.position inside update-function?

there no way can access variable defined privately in method.

but can this..

in addinvader() method, below this

var attacker = skspritenode(imagenamed: "someimage") 

you want add following code

attacker.name = "attacker"  

you can set attacker.name want string.

setting allow find node in skscene , use so

now once have set ready move on update(currenttime: nstimeinterval) method. inside update(currenttime: nstimeinterval) add line of code before else

let attacker =  self.childnodewithname("attacker") as! skspritenode 

what loops through of children in skscene , looks 1 name called attacker or whatever set. if attacker node not child of skscene , child of variable example attackerparent can this

let attcker = attackerparent.childnodewithname("attacker") as! skspritenode 

so update function this

let attacker = self.childnodewithname("attacker") as! skspritenode  if attacker.position.y < cgfloat(size.height/5*2) {      attacker.texture = sktexture(imagenamed: "someotherimage")  } 

also quick tip, if want change texture of skspritenode so

var texture = sktexture(imagenamed : "someotherimage") attacker.texture = texture 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -