我是 SpriteKit 的新手,我正在学习一些教程,但是我遇到了教程中没有提到的问题。 这是我要做的,当我每次触摸屏幕时,场景从 A 切换到 B,然后重复。
我有 GameScene.swift 和 MenuScene.swift,几乎相同的代码。 (Xcode 版本 7.3.1) 游戏场景.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) {
}
}
菜单场景.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) {
}
}
问题是当我运行代码时,它显示了蓝色背景和文本“hello world”,这正是我所期望的,我触摸屏幕,背景变为红色,但文本消失了,然后我再次触摸,背景变回蓝色,但文字仍然消失,我找不到为什么?
最佳答案
您需要设置场景大小和缩放模式。试试这个代码:
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) {
}
}
关于swift - spritekit + swift,改变场景效果不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420383/