在我的SpriteKit游戏的标题屏幕中,我想要SKLabelNode
显示"播放"。但是,当我运行下面的代码时,节点不会添加到场景中。
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)
}
我该怎么做才能使这项工作?
答案 0 :(得分:0)
您没有正确设置标签的位置,因此它可能不在屏幕上。你的代码适合我。试试这个:
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