Swift 3 SpriteKit主菜单

时间:2016-11-07 00:37:02

标签: swift sprite-kit swift3

我正在使用SpriteKit并尝试使用主菜单制作一个简单的游戏。我已经制作了游戏但是我在创建主菜单时遇到了麻烦。

下面是我的主菜单代码,我希望它改为gameScene并启动我的游戏。

import SpriteKit

class MenuScene: SKScene {

    var aButton = SKShapeNode(circleOfRadius: 50)

    override func didMove(to view: SKView) {
        aButton.fillColor = SKColor.red
        aButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
        self.addChild(aButton)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let scene = GameScene(fileNamed: "aButton")
        scene?.scaleMode = .aspectFill
        view!.presentScene(scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
    }
}

2 个答案:

答案 0 :(得分:1)

尝试更改此行:

let scene = GameScene(fileNamed: "aButton")

对此:

let scene = GameScene(size: self.scene.size)

第一行转换为名为“aButton”的.SKS文件。我假设你一旦触摸按钮就试图转换。

为此,首先给按钮命名:

aButton.name = "button"

如果被触摸则转换。您的整个touchesBegan方法应如下所示:

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

    let location = touches.first?.locationInNode(self)
    let touchedNode = self.nodeAtPoint(location)

    if touchedNode.name == "button" {
        let newScene = GameScene(size: self.scene.size)
        newScene.scaleMode = .aspectFill
        view!.presentScene(newScene, transition: SKTransition.doorsOpenVertical(withDuration: 1))
    }
}

答案 1 :(得分:0)

我发布了一个相当全面但基本的例子,主题菜单是SKSpriteKit storyboard以上here的Swift3。关于你试图以编程方式而不是SKViews方式尝试这样做的问题,我也只是复制粘贴下面的相关部分:

自定义SKViews

让我们说,就像这个M.W.E.一样,你想要一个菜单​​,一个难度和一个游戏场景。

然后,您可以制作一系列自定义override func viewDidLoad() { super.viewDidLoad() let menuScene = MenuScene(size: view.bounds.size) let skView = view as! SKView skView.showsFPS = true skView.showsNodeCount = true skView.ignoresSiblingOrder = true menuScene.scaleMode = .resizeFill skView.presentScene(menuScene) } 来进行转换。

enter image description here

GameViewController

此代码加载menuScene:

class MenuScene: SKScene {

    let playButton = SKLabelNode()


    override init(size: CGSize) {
        super.init(size: size)

        backgroundColor = SKColor.white

        playButton.fontColor = SKColor.black
        playButton.text = "play"

        playButton.position = CGPoint(x: size.width / 2, y: size.height / 2)

        addChild(playButton)

    }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let touchLocation = touch!.location(in: self)

        if playButton.contains(touchLocation) {

            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let difficultyScene = DifficultyScene(size: self.size)
            self.view?.presentScene(difficultyScene, transition: reveal)

        }

    }


}

MenuScene

class DifficultyScene: SKScene {

    let easyButton = SKLabelNode()
    let hardButton = SKLabelNode()
    let menuButton = SKLabelNode()


    override init(size: CGSize) {
        super.init(size: size)

        backgroundColor = SKColor.white

        easyButton.fontColor = SKColor.black
        easyButton.text = "easy"

        hardButton.fontColor = SKColor.black
        hardButton.text = "hard"

        menuButton.fontColor = SKColor.black
        menuButton.text = "menu"

        easyButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
        hardButton.position = CGPoint(x: size.width / 2, y: size.height / 2 - easyButton.fontSize * 2)
        menuButton.position = CGPoint(x: size.width / 4 * 3, y: size.height / 4)

        addChild(easyButton)
        addChild(hardButton)
        addChild(menuButton)

    }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let touchLocation = touch!.location(in: self)

        if easyButton.contains(touchLocation) {
            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let gameScene = GameScene(size: self.size, difficulty: easyButton.text!)
            self.view?.presentScene(gameScene, transition: reveal)
        }

        if hardButton.contains(touchLocation) {
            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let gameScene = GameScene(size: self.size, difficulty: hardButton.text!)
            self.view?.presentScene(gameScene, transition: reveal)
        }

        if menuButton.contains(touchLocation){
            let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
            let menuScene = MenuScene(size: self.size)
            self.view?.presentScene(menuScene, transition: reveal)
        }

    }


}

DifficultyScene

GameScene

GameScene

将此添加到您的init(size: CGSize, difficulty: String) { super.init(size: size) gameDifficulty = difficulty } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

UIWebView