Developingdeveloper Developingdeveloper - 1 year ago 65
Swift Question

Accesing local defined variable in other functions - Swift

I am making a game kind of like Space Invaders.
I have a function that creates invaders at a random


Inside my
, I have defined the attacker as a
SKSpriteNode(imageNamed: "someImage")

This have to be defined inside the function, since there are added a new
every second.

My problem is inside another function:

override func update(currentTime: NSTimeInterval) {
if attacker.position.y < CGFloat(size.height/5*2)
attacker.texture = SKTexture(imageNamed: "someOtherImage")

Since the
constant is local, the
update(currentTime: NSTimeInterval)
function can`t access it.

How can I access the
inside the update-function?

Answer Source

I created a global array called attackers. Then I simply added the each attacker to the array.

With an algorythm, I made sure the updatefunction was keeping track of the right attacker.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download