swift - spritekit + swift,改变场景效果不好

标签 swift sprite-kit

我是 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/

相关文章:

ios - 如何解决此错误: Could not cast value of type '__NSCFString' (0x10354a248) to 'UIImage' (0x104c42b48)

ios - 有没有兼容Java和Swift的格式说明?

ios - accessibilityElementDidBecomeFocused 没有被调用

ios - Spritekit场景 anchor 影响子节点定位

Swift 3 子弹发射延迟

ios - 如何在一个 NSFetchRequest 中获取核心数据关系?

ios - Xamarin IOS CrossMediaManager 后台视频播放

xcode - swift "Start Game Menu"

swift - SpriteKit + Swift - 如何使用缓动移动节点?

swift - 每次呈现新场景时,SpriteKit 内存都会增加