swift - How do I call a function from another scene? -
my game unlocks level every-time beat level. problem im having when user beats level 1 want show button of level 2 thats in gamescene. when call function unlockleveltwo() in level1.swift file doesn't show in gamescene.swift file. doing wrong?
//gamescene.swift func unlockleveltwo() { let fadein = skaction.fadeinwithduration(0.5) leveltwo.position = cgpointmake(self.size.width / 2.0, self.size.height / 2.2) leveltwo.zposition = 20 leveltwo.setscale(0.8) leveltwo.alpha = 0 leveltwo.runaction(fadein) leveltwo.name = "leveltwo" addchild(leveltwo) } //level1.swift if firstbody.categorybitmask == herocategory && sixthbody.categorybitmask == goldkeycategory{ //calling function gamescene unlock leveltwo button. var gamescene = gamescene() gamescene.unlockleveltwo() }
this function:
if firstbody.categorybitmask == herocategory && sixthbody.categorybitmask == goldkeycategory{ //calling function gamescene unlock leveltwo button. var gamescene = gamescene() gamescene.unlockleveltwo() } is creating new gamescene object , adding childnode "leveltwo" gamescene. instead, need call unlockleveltwo() on actual gamescene presented user.
i imagine somewhere in level1.swift there reference current gamescene (the 1 user interacting with)? call function on one.
edit:
in essence, must keep reference original gamescene object somewhere in code, henceforth refer myscene. way, when in level1.swift, can reference myscene , add buttons, levels, whatever without creating new 1 did here:
var gamescene = gamescene() gamescene.unlockleveltwo()
instead, call
myscene.unlockleveltwo().
if level1 view of sort, or object gets created, in init function pass in gamescene object , set so
class level1 : (type) { var myscene: gamescene! init(myscene: gamescene) { self.myscene = myscene) } } something that, hope helps!
Comments
Post a Comment