Difference between GameViewController and SKScenes

I am developing a game using SpriteKit and Swift, but I seem to be having trouble GameViewController out what the real differences are between GameViewController and any of my SKScene s. I'm trying to understand the differences because I want to implement a GameCenter or a local leaderboard in my game, but in all the tutorials that I find (for example, this: Game Center Leaderboards! (Swift 2 in Xcode)), they have all the logic in GameViewController as they work with applications with one representation. I have problems understanding the attitude when I read documents, so any help would be great. Ultimately, I want to be able to display and move data to and from GameCenter in one of my scenes, such as GameOverScene . Thanks for any help!

+6
source share
4 answers

Here is some good information to start with:

Scheme of each frame in SK:

enter image description here


So, you see that SKScene is a class with all interesting things like Nodes and Actions, and where everything (important to you) happens. You can generate these scenes through the editor, but then you probably need to create a new .swift file to go with it (since each scene can have its own logic).

The editor is just a “shortcut” to initialize a bunch of things, and frankly, you can create complete games with a little code (but you will know very quickly what you want more)

So, in this code, where you declare a GameScene or PauseScreen (which are basically just class declarations that inherit from SKScene), you will quickly find this line talking about something that isnt:

override func didMoveToView(view: SKView) .. it calls SKView ... what is it and where did it come from?

(Read about SKView here and look at its inheritance):

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKView/index.html#//apple_ref/occ/cl/SKView


We find this SKView declaration in the GameViewController file (this is just a class), note that it is similar to regular iOS applications, as it inherits from the UIViewController:

 override func viewDidLoad() { super.viewDidLoad() if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill skView.presentScene(scene) } 

Again, this method is declared in GameViewController.swift, which is basically the following: class GameViewController: UIViewController


So how does all this relate to iOS and SpriteKit apps? Well, they are all scattered over each other:

IOS app anatomy:

anatomy

Basically, from right to left you have a Window that (correct me, if not) AppDelegate, then a ViewController, and then your View, which has all the cool stuff (Storyboards sit inside the View, just like SKScenes sit inside the view. ... Labels, nodes or buttons, all sit inside their respective classes ((view)))

All of this is a great inheritance sandwich.


Check out the Apple websites for more information.

https://developer.apple.com/library/safari/documentation/UserExperience/Conceptual/MobileHIG/ContentViews.html#//apple_ref/doc/uid/TP40006556-CH13-SW1

https://developer.apple.com/spritekit/

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SpriteKitFramework_Ref/

https://developer.apple.com/library/safari/documentation/UserExperience/Conceptual/MobileHIG/Anatomy.html

Basically, this is all a class inherited from a class inherited from a class, etc., and so on ... It can become messy. You can also see these inheritances in Xcode on CMD + by clicking on them, which will lead you to the source file.

Goodluck with your exploration and adventure at SpriteKit :)

+4
source

Your game should have only one GameViewController. SKScenes are scenes in which a game moves between characters. To.

For example, the main menu screen? This is SKScene. The main gameplay? This is SKScene. Game screen This is SKScene.

GameViewController initializes the entire view in which the game will be supported, therefore the view. SKScenes are simply scenes that are placed on top of a view. You should look at the tutorial that uses SKScenes.

Here's how to make the game center work with the latest Swift 2.2.

Add this function anywhere in the GameViewController class, and then just call it right after super.viewDidLoad ().

 func authenticateLocalPlayer() { let localPlayer = GKLocalPlayer.localPlayer() localPlayer.authenticateHandler = {(viewController, error) -> Void in if (viewController != nil) { self.presentViewController(viewController!, animated: true, completion: nil) } else { print((GKLocalPlayer.localPlayer().authenticated)) } } } 

Add the following functions to your SKScene class file. Remember to import GameKit. Just call showLeader () when you want to display the leaderboard.

 func showLeader() { let viewControllerVar = self.view?.window?.rootViewController let gKGCViewController = GKGameCenterViewController() gKGCViewController.gameCenterDelegate = self viewControllerVar?.presentViewController(gKGCViewController, animated: true, completion: nil) } func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) { gameCenterViewController.dismissViewControllerAnimated(true, completion: nil) } 

And this is the sample that I have, how the score is saved in the game center.

 func saveHighscore(gameScore: Int) { print("Player has been authenticated.") if GKLocalPlayer.localPlayer().authenticated { let scoreReporter = GKScore(leaderboardIdentifier: "YOUR_LEADERBOARD_ID") scoreReporter.value = Int64(gameScore) let scoreArray: [GKScore] = [scoreReporter] GKScore.reportScores(scoreArray, withCompletionHandler: {error -> Void in if error != nil { print("An error has occured: \(error)") } }) } } 
+4
source

It all depends on how you develop your application and what technologies you want to use.

If you want to create an application in a 100% set of sprites, you will treat your UIViewController as a wrapper containing your Sprite Kit application. The only time you need to touch on this is when you need to do what the SpriteKit script should not do, for example, create gesture controls and what not.

However, there are ways to use multiple view controllers with sprite set elements. Perhaps you are creating a business application and decide to include a small game in it.

IMO the best way to think about this in terms of web design is to think of your View controller as an HTML page and think of your Scene as the flash / silverlight / unity / etc game player that you embed on your website. Sometimes you want this player to be in full screen mode, and sometimes you don’t do it, it comes down to the design of the application. In full screen mode, we do not need any other components, so the player can do all the work. But what if we attach a guide for the link on the page. We would not want this in the game, we want this beyond. This link will open a new page unrelated to the old page and will not be used for game player components.

Now for your situation with Game Center, this is complicated. The game center was built before the Sprite Kit, so all its functions are built on UIKit. But Game Center also allows you to customize, so you do not need to use the capabilities of UIKit. Of course, you will have to do all the work and then display the information inside your scene using the Sprite Kit objects.

To make life easier for you, you would include all the built-in code needed in your view controller, then what you do is create a delegate that the scene knows about and assign your view controller to that delegate. Now, Game Scene can access any element of this view controller that you allow, for example, present leaderboards or skip leaderboards. Study this tutorial fully, it will help you learn all that you need to achieve what you want. https://www.raywenderlich.com/115300/swift-2-tutorial-part-3-tuples-protocols-delegates-and-table-views

+4
source

In MVC, the controller acts as a coordinator, a bit like a conductor in an orchestra. My preference is that the scenes just do what they were designed to realize the game. When the scene is completed, the final task is to notify the controller (using the delegate template) that the scene is completed. Then the controller must decide what happens next, that is, the transition to the next scene or game.

0
source

All Articles