sprite kit - Best way to switch back and forth between two variables when switched swift -


i have variable, gamemode, switch between vertical , horizontal whenever screen tapped. issue having takes lower variable , gamemode displays horizontal. how fix switches whenever touchesbegan method called? appreciated!

here code issue having:

override func touchesbegan(_ touches: set<uitouch>, event: uievent?) {       if gamemode == "horizontal" {          gamemode = "vertical"      }       if gamemode == "vertical" {          gamemode = "horizontal"      }       print(gamemode) } 

it happens because if gamemode "horizontal", set gamemode "vertical" , after gamemode == "vertical" returns true, set gamemode "horizontal".

try code:

override func touchesbegan(_ touches: set<uitouch>, event: uievent?) {     if gamemode == "horizontal" {          gamemode = "vertical"     } else {         gamemode = "horizontal"     }      print(gamemode) } 

you should use enum instead of strings.

example:

enum gamemode {     case horizontal     case vertical      mutating func toggle() {         self = self == .horizontal ? .vertical : .horizontal     } }  var gamemode: gamemode = .horizontal  override func touchesbegan(_ touches: set<uitouch>, event: uievent?) {     gamemode.toggle()     print(gamemode) } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -