我正在使用SpriteKit,试图用主菜单制作一个简单的游戏。我已经做了这个游戏,但在创建主菜单时遇到了问题。
下面是主菜单的代码,我希望它切换到游戏场景并开始我的游戏。

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

最佳答案

尝试更改此行:

let scene = GameScene(fileNamed: "aButton")

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

第一行转换为名为“abuton”的.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))
    }
}

关于swift - Swift 3 SpriteKit主菜单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40456219/

10-09 01:19