我使用spritekit和swift在xcode中创建了一个非常简单的游戏。我已经完成了对实际游戏本身的编码。现在我想创建一个主菜单,这样当游戏打开时,会有一个带按钮的菜单进入设置或开始游戏。我不知道怎么做到这一点。我应该使用故事板吗?如果是这样,我如何在xcode中实现它。谢谢大家:)
答案 0 :(得分:11)
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)
}
}
}
}
}
答案 1 :(得分:5)
创建两个新文件。一个精灵套件场景和一个可可触摸文件。将它们命名为相同,例如MenuScene。要创建文件,请单击xcode项目侧栏中的文件夹。然后在运行应用程序时让MenuScene显示进入GameViewController文件。 viewDidLoad
下有一行。 if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
将此更改为if let scene = MenuScene.unarchiveFromFile("MenuScene") as? Menu Scene {
然后在您的MenuScene中,您必须有一个按钮,引导您进入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)
}
}
}
}
询问您是否需要进一步解释。