Search code examples
iosswiftxcodesprite-kitskscene

Calling GameScene in another Class with parameter in SWIFT


I am so frustrated trying to get this working for 2 hours now!!!

I have two classes GameScene and Setup and I want to print the frame of GameScene in my Setup class.

class Setup {

   static var gameScene = GameScene()
   static func whatsFrame() {
      print("\(gameScene.frame.maxX)")
}


class GameScene: SKScene {

   override func didMove(to view: SKView) {
      print("\(self.frame.maxX)")
      Setup.whatsFrame()
   }
}

It should be so simple but the console gives following answer:

320.0 and -0.0

Solution

  • Try passing GameScene frame-information via parameters to your Setup class like:

    In your GameScene didMove() function:

            Setup.whatsFrame(frameOfGameSceneMaxX: self.frame.maxX,
                                frameOfGameSceneMinX: self.frame.minX)
    

    And in your Setup class:

       static func whatsFrame(frameOfGameSceneMaxX: CGFloat, frameOfGameSceneMinX: CGFloat) {
           print("\(frameOfGameSceneMaxX)")
           print("\(frameOfGameSceneMinX)")
    }