Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a main menu for a spritekit game made with swift in xcode?

I created a really simple game in xcode using spritekit and swift. I have finished coding the actual game itself. Now I want to create a main menu so that when the game opens, there will be a menu with buttons either going into settings or starting the game. I have no idea how I can do this. Should I use storyboard? And if so, how can i implement it in xcode. Thanks everyone :)

like image 615
King_Cui Avatar asked Aug 08 '15 02:08

King_Cui


People also ask

Can you make a game using Swift?

Swift is the perfect choice for game development. Developers are intrigued by Swift and want to make use of new features to develop their best games yet. Packed with best practices and easy-to-use examples, this book leads you step by step through the development of your first Swift game.

Can you use SpriteKit with SwiftUI?

Even though the default Game Xcode Template creates the project based on a UIKit application, you can create a SwiftUI app and put your SpriteKit game inside it without any hustle thanks to the SpriteView view!

What is SpriteKit in Xcode?

What is SpriteKit? SpriteKit is a powerful 2D sprite-based framework for games development from Apple. SpriteKit uses SKView which is a scene, it is the visual that you see on your screen. For those who are familiar with making iOS App, it is similar to Storyboard.

What is SpriteKit in Swift?

SpriteKit is a general-purpose framework for drawing shapes, particles, text, images, and video in two dimensions. It leverages Metal to achieve high-performance rendering, while offering a simple programming interface to make it easy to create games and other graphics-intensive apps.


2 Answers

Swift 3.0

import SpriteKit

class MenuScene: SKScene {

    var playButton = SKSpriteNode()
    let playButtonTex = SKTexture(imageNamed: "play")

    override func didMove(to view: SKView) {

        playButton = SKSpriteNode(texture: playButtonTex)
        playButton.position = CGPoint(x: frame.midX, y: frame.midY)
        self.addChild(playButton)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let pos = touch.location(in: self)
            let node = self.atPoint(pos)

            if node == playButton {
                if let view = view {
                    let transition:SKTransition = SKTransition.fade(withDuration: 1)
                    let scene:SKScene = GameScene(size: self.size)
                    self.view?.presentScene(scene, transition: transition)
                }
            }
        }
    }
}
like image 198
ZachtheBoB Avatar answered Sep 16 '22 19:09

ZachtheBoB


Create two new files. One sprite kit scene and one cocoa touch file. Name them the same so for example MenuScene. To create the files you click on the folder in the sidebar of your xcode project. Then to have the MenuScene show when you run your app go into the GameViewController file. There is a line under viewDidLoad. if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { change this into if let scene = MenuScene.unarchiveFromFile("MenuScene") as? Menu Scene { Then in your MenuScene you have to have a button that leads you to your GameScene

import SpriteKit

class MenuScene: SKScene {

var playButton = SKSpriteNode()
let playButtonTex = SKTexture(imageNamed: "play")

override func didMoveToView(view: SKView) {

    playButton = SKSpriteNode(texture: playButtonTex)
    playButton.position = CGPointMake(frame.MidX, frame.midY)
    self.addChild(playButton)


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {


    if let touch = touches.first as? UITouch {
        let pos = touch.locationInNode(self)
        let node = self.nodeAtPoint(pos)

        if node == playButton {
            if let view = view {
                let scene = GameScene.unarchiveFromFile("GameScene") as! GameScene
                scene.scaleMode = SKSceneScaleMode.AspectFill
                view.presentScene(scene)   
            }   
        }   
    }       
}

Ask If you need further explanation.

like image 30
Miles Militis Avatar answered Sep 17 '22 19:09

Miles Militis