SKLabelNode 未在 SKScene 中创建 – Swift

SKLabelNode not being created in SKScene – Swift

在我的 SpriteKit 游戏的标题屏幕中,我想要 SKLabelNode 显示 "Play"。但是,当我运行下面的代码时,节点没有添加到场景中。

import UIKit
import SpriteKit

class TitleScreen: SKScene {

var title: SKLabelNode!

override func didMoveToView(view: SKView) {

    backgroundColor = SKColor(colorLiteralRed: 0.4, green: 0.835, blue: 1, alpha: 1)

    //The color is a light blue, so the node should not be hidden by the color of the screen.

    title = SKLabelNode()
    title.text = "Play"
    title.color = SKColor.blackColor()
    title.fontSize = 70

    self.addChild(title)

}

我应该怎么做才能完成这项工作?

您没有正确设置标签的位置,所以它可能在屏幕之外。你的代码对我有用。试试这个:

override func didMoveToView(view: SKView) {

        backgroundColor = SKColor(colorLiteralRed: 0.4, green: 0.835, blue: 1, alpha: 1)

        //The color is a light blue, so the node should not be hidden by the color of the screen.

        title = SKLabelNode()
        title.position = CGPoint(x: frame.midX, y: frame.midY)
        title.text = "Play"
        title.color = SKColor.blackColor()
        title.fontSize = 70

        self.addChild(title)
    }

请注意,从 .sks 文件加载场景时,它的默认大小为 1024x768。如果你想改变它,在创建时明确设置场景的大小。如果你想匹配视图的大小,你可以这样做:

scene.size = yourSkView.bounds.size