ios - Changing a property of an object from GameViewController.swift in GameScene.swift -
i'm new swift, sorry if seems obvious question.
i'm trying write game in swift, , have designed label , play button in storyboard.
i've added references these objects in gameviewcontroller.swift file seen here:
// mark: objects @iboutlet var gamelabel: uilabel! @iboutlet var playbutton: uiimageview! // mark: actions @ibaction func playbuttontapped(sender: uitapgesturerecognizer) { gamescene().gamestart() }
this belongs gameviewcontroller class: class gameviewcontroller: uiviewcontroller
.
however, main game takes place in gamescene.swift. involves me having hide label , image game starts. i'm unsure how set properties these objects hide, since if did following in gamescene.swift:
gameviewcontroller().playbutton.hidden = true
...the game crashes fatal error. error says: fatal error: unexpectedly found nil while unwrapping optional value.
does have suggestion problem? appreciated. sorry if information difficult understand.
cheers.
you creating new instance of 'gameviewcontroller(), want want use existent one. if want create one
gameviewcontroller`, case, use static instance variable.
in gameviewcontroller
, put:
static var instance: gameviewcontroller!
in it's viewdidload()
, put:
gameviewcontroller.instance = self
finally, in gamescene
, switch gameviewcontroller()
gameviewcontroller.instance
.
Comments
Post a Comment