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.

main.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 onegameviewcontroller`, 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

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -