如何在xcode中使用swift创建spritekit游戏的主菜单? [英] How to create a main menu for a spritekit game made with swift in xcode?

查看:102
本文介绍了如何在xcode中使用swift创建spritekit游戏的主菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用spritekit和swift在xcode中创建了一个非常简单的游戏。我已经完成了对实际游戏本身的编码。现在我想创建一个主菜单,这样当游戏打开时,会有一个带按钮的菜单进入设置或开始游戏。我不知道我怎么能这样做。我应该使用故事板吗?如果是这样,我如何在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 :)

推荐答案

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)
                }
            }
        }
    }
}

这篇关于如何在xcode中使用swift创建spritekit游戏的主菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆