2016-07-17 5 views
1

私はSpriteKitが新しく、チュートリアルに従っていますが、チュートリアルでは触れていない問題があります。 ここで私がやることは、画面をタッチするたびに、シーンがAからBに変わり、繰り返されます。spritekit + swift、change scene doesnot good

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) { 
 

 
    } 
 
}

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) { 
 
     
 
    } 
 
}

問題がありますコードでは、青い背景と私が期待していたテキスト "hello world"、そして私は画面に触れた、背景は赤に変わったが、テキストは消えて、私は再び触れ、背景は青に戻ったが、テキストはまだ消えている。なぜ見つからないの?

答えて

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) { 

} 
} 
関連する問題