Xcode 6 - SpriteKit Scene won't load when I hit the Restart Button I made

79 views Asked by At

Here is the code for my "EndScene", which loads when the player loses. I am trying to have the restart button reload the GameScene, allowing the player to try again. Any help?

import Foundation
import SpriteKit

class EndScene : SKScene {

    var RestartBtn : UIButton!
    var Highscore : Int!
    var ScoreLbl : UILabel!
    var HighscoreLbl : UILabel!

    override func didMoveToView(view: SKView) {
        scene?.backgroundColor = UIColor.blackColor()

        var ScoreDefault = NSUserDefaults.standardUserDefaults()
        var Score = ScoreDefault.valueForKey("Score") as! NSInteger

        var HighscoreDefault = NSUserDefaults.standardUserDefaults()
        Highscore = HighscoreDefault.valueForKey("Highscore") as! NSInteger

        ScoreLbl = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
        ScoreLbl.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.width / 4)
        ScoreLbl.text = "\(Score)"
        self.view?.addSubview(ScoreLbl)
        ScoreLbl.textColor = UIColor.whiteColor()

        HighscoreLbl = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
        HighscoreLbl.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.width / 2)
        HighscoreLbl.text = "\(Highscore)"
        self.view?.addSubview(HighscoreLbl)
        HighscoreLbl.textColor = UIColor.whiteColor()

        RestartBtn = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
        RestartBtn.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.width / 7)

        RestartBtn.setTitle("Restart", forState: UIControlState.Normal)
        RestartBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        RestartBtn.addTarget(self, action: Selector("Restart"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view?.addSubview(RestartBtn)





    }

    func Restart(){
        self.view?.presentScene(GameScene(), transition: SKTransition.crossFadeWithDuration(0.3))
        RestartBtn.removeFromSuperview()
        ScoreLbl.removeFromSuperview()
        HighscoreLbl.removeFromSuperview()

    }



}
0

There are 0 answers