You can't - the sun
variable is local to the hello
function, and it doesn't exist outside its scope.
If collide
is invoked from hello
, you can just pass it as an argument:
func collide(sun: SKSpriteNode) {
if (CGRectIntersectsRect(player.frame, sun.frame)) {
[EndGame];
}
}
func hello() {
let sun = SKSpriteNode(imageNamed: "Sun")
...
collide(sun)
}
Otherwise if, as I think, these are instance methods of a class, just turn the sun
variable into an instance property:
class Test {
private var sun: SKSpriteNode?
func collide(sun: SKSpriteNode) {
if let sun = self.sun {
if (CGRectIntersectsRect(player.frame, sun.frame)) {
[EndGame];
}
}
}
func hello() {
self.sun = SKSpriteNode(imageNamed: "Sun")
}
}