Binding GameViewController.swift to GameScene.swift

I created user interface elements on main.storyboard that I need to hide until the game is over and as soon as the player touches the screen to remove it. Main.storyboard is related to GameViewController, so all my IBOutlets and IBActions are there, and all my game code is in GameScene. How can I associate a view controller with a scene so that the pop-up image and buttons are displayed only when the game is over. I would really appreciate some help, I've been stuck with this for quite some time.

+4
source share
2 answers

This is apparently a fairly common problem that people encounter with SpriteKit games, so it helps to understand the differences between SpriteKit games and UIKit applications.

When you make a regular UIKit application, for example. YouTube, Facebook, you will use ViewControllers, CollectionViews, Views, etc. For each screen / menu that you see (main screen, channel screen, subscription channel screen, etc.). Therefore, you should use the UIKit API for such as UIButtons, UIImageViews, UILabels, UIViews, UICollectionViews, etc. For this, visually we will use storyboards.

SpriteKit, , -. SKScenes , (MenuScene, SettingsScene, GameScene, GameOverScene ..) 1 ViewController (GameViewController). GameViewController, SKView, SKScenes.

, SKScenes, API- SpriteKit, SKLabelNodes, SKSpriteNodes, SKNodes .. , SpriteKit, .

, , SKScene, , GameViewController, - SKScenes. GameViewController , . 1 (GameScene → GameOverScene).

GameViewController , , SKScenes, GameViewController, SKScenes. / , .

SpriteKit, -

 class GameScene: SKScene {

  lazy var scoreLabel: SKLabelNode = {
      let label = SKLabelNode(fontNamed: "HelveticaNeue")
      label.text = "SomeText"
      label.fontSize = 22
      label.fontColor = .yellow
      label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
      return label
  }()

  override func didMove(to view: SKView) {

      addChild(scoreLabel)
  }
} 

, , , SKSpriteNode , touchhesBegan touchEnded SKAction .

enum ButtonName: String {
     case play
     case share
}

class GameScene: SKScene {

       lazy var shareButton: SKSpriteNode = {
            let button = SKSpriteNode(imageNamed: "ShareButton")
            button.name = ButtonName.share.rawValue
            button.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
            return button
        }()

       override func didMove(to view: SKView) {

             addChild(shareButton)
       }

       /// Touches began
       override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

             for touch in touches {
                 let location = touch.location(in: self)
                 let node = atPoint(location)

                 if let nodeName = node.name {
                      switch nodeName {
                      case ButtonName.play.rawValue:
                          // run some SKAction animation and some code
                      case ButtonName.share.rawValue:

                          let action1 = SKAction.scale(to: 0.9, duration: 0.2)
                          let action2 = SKAction.scale(to: 1, duration: 0.2)
                          let action3 = SKAction.run { [weak self] in
                             self?.openShareMenu(value: "\(self!.score)", image: nil) // image is nil in this example, if you use a image just create a UIImage and pass it into the method
                          }
                          let sequence = SKAction.sequence([action1, action2, action3])
                          node.run(sequence)

                      default: 
                          break
                    }
               }
          }
     }
} 

, , https://nathandemick.com/2014/09/buttons-sprite-kit-using-swift/

DemoBots Apple .

, , .. , .

UIActivityController API, . 1 , . , , SKScene, .

func openShareMenu(value: String, image: UIImage?) {
    guard let view = view else { return }

    // Activity items
    var activityItems = [AnyObject]()

    // Text
    let text = "Can you beat my score " + value
    activityItems.append(text as AnyObject)

    // Add image if valid
    if let image = image {
        activityItems.append(image)
    }

    // Activity controller
    let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

    // iPad settings
    if Device.isPad {
        activityController.popoverPresentationController?.sourceView = view
        activityController.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
        activityController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)
    }

    // Excluded activity types
    activityController.excludedActivityTypes = [
        UIActivityType.airDrop,
        UIActivityType.print,
        UIActivityType.assignToContact,
        UIActivityType.addToReadingList,
    ]

    // Present
    view.window?.rootViewController?.present(activityController, animated: true)
}

, (. )

openShareMenu(value: "\(self.score)", image: SOMEUIIMAGE)

,

+4

GameViewController GameScene,

class GameScene: SKScene, SKPhysicsContactDelegate {

    var referenceOfGameViewController : GameViewController!

}

GameViewController

   class GameViewController: UIViewController {

       override func viewDidLoad() {
      super.viewDidLoad()

    if let view = self.view as! SKView? {
    // Load the SKScene from 'GameScene.sks'
     if let scene = GameScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill
            scene.referenceOfGameViewController = self
            // Present the scene
            view.presentScene(scene)
        }


    view.ignoresSiblingOrder = true

    view.showsFPS = true
    view.showsNodeCount = true
    }
  }
}

, GameScene

        scene.referenceOfGameViewController = self

GameScene GameViewController

referenceOfGameViewController.fbButton.hidden = false
referenceOfGameViewController.gameOverPopUP.hidden = false
+2

Source: https://habr.com/ru/post/1661128/


All Articles