Reputation: 63
I have three scenes in my game the first one is red the second one is yellow and the third one is blue so if the user touch the red one present to yellow scene and if they touch the yellow one present to blue scene and the blue scene return you to red scene , in real game MenuScene , WaitingScene and PlayScene ,
if I touch in red scene every method in yellow scene being called but still the red node is visible and if I touch one more time the methods in blue scene called and the yellow node is called , basically every time present to next scene the view or be late one step .
Red Scene and extension :
import SpriteKit
import GameplayKit
class RedScene : SKScene {
override func sceneDidLoad() {
print("red")
let red = SKSpriteNode(color: .red, size: CGSize(width: self.frame.size.width, height: self.frame.size.height))
red.position = CGPoint(x: 0, y: 0)
self.addChild(red)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
present(Scene: "YellowScene")
}
}
extension SKScene{
func present(Scene scene : String){
if let scene = SKScene(fileNamed: scene){
scene.scaleMode = .aspectFill
self.view?.presentScene(scene)
}
}
}`
Yellow Scene :
import SpriteKit
class YellowScene : SKScene{
override func sceneDidLoad() {
print("yellow")
let yellow = SKSpriteNode(color: .yellow, size: CGSize(width: self.frame.size.width, height: self.frame.size.height))
yellow.position = CGPoint(x: 0, y: 0)
self.addChild(yellow)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
present(Scene: "BlueScene")
}
}
Blue Scene :
import SpriteKit
class BlueScene : SKScene{
override func sceneDidLoad() {
print("blue")
let blue = SKSpriteNode(color: .blue, size: CGSize(width: self.frame.size.width, height: self.frame.size.height))
blue.position = CGPoint(x: 0, y: 0)
self.addChild(blue)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
present(Scene: "RedScene")
}
}
Image : now I should be in Blue Scene but yellow node is still here!!
BTW : when I rotate the simulator or click in home button and then back to the game bleu node pop up.
Upvotes: 1
Views: 129
Reputation: 3182
Try to load scene using actual classes.
To open Yellow scene:
if let scene = YellowScene(fileNamed: "YellowScene"){
scene.scaleMode = .aspectFill
self.view?.presentScene(scene)
}
To open Blue scene:
if let scene = BlueScene(fileNamed: "BlueScene"){
scene.scaleMode = .aspectFill
self.view?.presentScene(scene)
}
Upvotes: 1