Reputation: 81
im having a trouble in scene transition at spritekit application written on swift 4
I have a gameover.swift file declaring EndScene class:
import Foundation
import SpriteKit
class EndScene: SKScene, SKPhysicsContactDelegate {
var RestartButton : UIButton!
var HighScore : Int!
var HighScoreLabel : UILabel!
var GameOverLabel : UILabel!
override func didMove(to view: SKView) {
self.scene?.backgroundColor = SKColor.white
RestartButton = UIButton(frame: CGRect(x:0, y:0, width: view.frame.size.width/3
, height: 30))
RestartButton.titleLabel?.adjustsFontSizeToFitWidth = true
RestartButton.center = CGPoint(x: view.frame.size.width/2 , y: view.frame.size.height/1.5)
RestartButton.setTitle("restart", for: UIControlState.normal)
RestartButton.showsTouchWhenHighlighted = true
RestartButton.setTitleColor(SKColor.black, for: UIControlState.normal)
RestartButton.addTarget(self, action: #selector(EndScene.Restart), for: UIControlEvents.touchUpInside)
self.view?.addSubview(RestartButton)
GameOverLabel = UILabel (frame: CGRect(x:0, y:0, width: view.frame.size.width/3.8, height: 30))
GameOverLabel.center = CGPoint(x: view.frame.size.width/2 , y: view.frame.size.height/10)
GameOverLabel.textColor = SKColor.red
GameOverLabel.text = "Game Over"
self.view?.addSubview(GameOverLabel)
}
func Restart(){
RestartButton.removeFromSuperview()
GameOverLabel.removeFromSuperview()
self.removeAllChildren()
self.view?.presentScene(GameScene(), transition: SKTransition.reveal(with: .up, duration: 0.3))
}
}
GameScene is a class declared in GameScene.swift file. It contains declaration of class describing game process.
The problem is:
when the game starts from GameScene it works correct (all objects are drown and physics work) but when i restart game from EndScene it works incorrect (1 object is drown from time to time but every other objects aren't drown, even background color isn't drown).
What in general can be with it?
Upvotes: 1
Views: 40
Reputation: 270980
You shouldn't just create a GameScene
by calling its parameterless initializer. Look at how GameScene
is presented in the generated GameViewController.swift
file. You will see that it calls (fileNamed:)
initializer.
So change this line:
self.view?.presentScene(GameScene(), transition: SKTransition.reveal(with: .up, duration: 0.3))
To
self.view?.presentScene(GameScene(fileNamed: "GameScene"), transition: SKTransition.reveal(with: .up, duration: 0.3))
Upvotes: 2