您可以创建一个 SpriteKit 场景作为游戏的主菜单,并使用三个不同的按钮来过渡到每个游戏场景。您可以使用 SKLabelNode 创建标题,并使用 Sprite 或 Shape Node 作为场景的按钮。
我体验过将 UIButtons 和 UILabels 添加到 SpriteKit 场景中,在定位它们的问题上会变得非常技术性。由于 UI 对象位于视图上,而不是直接位于 SpriteKit 场景上。您可以将 SKSpriteNode 用作按钮,将 SKLabelNode 用作标题,用于菜单场景。
Sprite 套件场景放置在 UIView 上,并根据您定义的缩放模式进行缩放。 Apple 的默认缩放模式 .aspectFill 不需要调整 Sprite 套件对象在不同手机设备屏幕尺寸上的定位。
这是一个 SKSpriteNode 的自定义类,具有与按钮相同的功能。
import Foundation
import SpriteKit
class ButtonLabelNode : SKSpriteNode {
let buttonPressed: () -> ()
init(texture: SKTexture?, color: UIColor, size: CGSize, text: String, buttonPressed: @escaping () -> ()) {
self.buttonPressed = buttonPressed
super.init(texture: texture, color: color, size: size)
let label = SKLabelNode(fontNamed: "Futura")
label.fontSize = 50
label.fontColor = SKColor.red
label.position = CGPoint.init(x: 0.0, y: 0.0)
label.zPosition = 1
label.verticalAlignmentMode = .center
label.text = text
self.addChild(label)
self.isUserInteractionEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.alpha = 0.8
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.alpha = 1.0
buttonPressed()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
当在 SpriteNode 上开始触摸时,alpha 减小到 0.8,并在触摸结束时回到 1.0,给它与 UIButton 相同的视觉效果。在覆盖的函数“touchesEnded”中,有一个函数在每次按下按钮时都会被调用,该函数被添加到初始化程序中,并且可以在你的游戏场景中进行初始化。
override func didMove(to view: SKView) {
let labelNode = LabelNode(texture: nil, color: .white, size: CGSize.init(width: 200, height: 100), text: "Play", buttonPressed: playButton)
labelNode.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(labelNode)
}
func playButton()
{
print("play")
}
如果您想要不同形状或独特形状的按钮,您可以将 ShapeNode 子类化而不是 SpriteNode 并实现相同的功能,下面是一个如何将圆形 ShapeNode 创建为按钮的示例。
import Foundation
import SpriteKit
class SKShapeButton: SKShapeNode {
init(circleOfRadius: CGFloat) {
super.init()
let diameter = circleOfRadius * 2.0
self.path = CGPath(ellipseIn: CGRect(origin: CGPoint.init(x: 0.0 - circleOfRadius, y: 0.0 - circleOfRadius), size: CGSize(width: diameter, height: diameter)), transform: nil)
self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.alpha = 0.8
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.alpha = 1.0
}
}
然后在你的游戏场景中创建它的一个实例。
override func didMove(to view: SKView) {
let shapeButton = SKShapeButton(circleOfRadius: 50)
shapeButton.fillColor = .red
shapeButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(shapeButton)
}
要过渡到您的每个游戏场景,您可以实现 Game View Controller swift 文件中的代码,该代码呈现提供的游戏场景。
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}