【问题标题】:SKLabelNode not being created in SKScene – SwiftSKScene 中未创建 SKLabelNode – Swift
【发布时间】:2016-04-01 18:13:50
【问题描述】:

在我的 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)

}

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

【问题讨论】:

  • 验证 didMoveToView 是否被调用

标签: ios swift sprite-kit swift2 skscene


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    相关资源
    最近更新 更多