我是SpriteKit的新手,我正在学习一些教程,但是我遇到了教程中没有提到的问题。 这就是我要做的事情,当我每次触摸屏幕时,场景从A变为B,然后重复。
我有GameScene.swift和MenuScene.swift,几乎是相同的代码。 (Xcode版本7.3.1) GameScene.swift:
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
createScene()
}
func createScene() {
self.backgroundColor = SKColor.blueColor()
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.name = "label"
myLabel.text = "Hello, World!"
myLabel.fontSize = 45
myLabel.position = CGPoint(x:500, y:300)
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let nextScene = MenuScene()
let doors = SKTransition.doorsOpenVerticalWithDuration(0.5)
self.view!.presentScene(nextScene, transition: doors)
}
override func update(currentTime: CFTimeInterval) {
}
}
&#13;
MenuScene.swift:
import SpriteKit
class MenuScene: SKScene {
override func didMoveToView(view: SKView) {
createScene()
}
func createScene() {
self.backgroundColor = SKColor.redColor()
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.name = "label"
myLabel.text = "Hello, World!"
myLabel.fontSize = 45
myLabel.position = CGPoint(x:500, y:300)
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let nextScene = GameScene()
let doors = SKTransition.doorsOpenVerticalWithDuration(0.5)
self.view!.presentScene(nextScene, transition: doors)
}
override func update(currentTime: CFTimeInterval) {
}
}
&#13;
问题是,当我运行代码时,它显示蓝色背景和文本&#34; hello world&#34;,这是我的预期,我触摸屏幕,背景变为红色,但是文字消失了,然后我再次触摸,背景变回蓝色,但文字仍然消失,我找不到原因?
答案 0 :(得分:0)
您需要设置场景和比例模式的大小。试试这段代码:
import SpriteKit
class MenuScene: SKScene {
override func didMoveToView(view: SKView) {
createScene()
}
func createScene() {
self.backgroundColor = SKColor.redColor()
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.name = "label"
myLabel.text = "Hello, World!"
myLabel.fontSize = 45
myLabel.position = CGPoint(x:500, y:300)
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let nextScene = GameScene(size: scene!.size)
nextScene.scaleMode = .AspectFill
let doors = SKTransition.doorsOpenVerticalWithDuration(0.5)
self.view!.presentScene(nextScene, transition: doors)
}
override func update(currentTime: CFTimeInterval) {
}
}
和
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
createScene()
}
func createScene() {
self.backgroundColor = SKColor.blueColor()
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.name = "label"
myLabel.text = "Hello, World!"
myLabel.fontSize = 45
myLabel.position = CGPoint(x:500, y:300)
myLabel.zPosition = 2.0
self.addChild(myLabel)
print(myLabel.position)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let nextScene = MenuScene(size: scene!.size)
nextScene.scaleMode = .AspectFill
let doors = SKTransition.doorsOpenVerticalWithDuration(0.5)
self.view!.presentScene(nextScene, transition: doors)
}
override func update(currentTime: CFTimeInterval) {
}
}