Xcode 6 SpriteKit Scene not Refreshing

77 views Asked by At

I am trying to reload the GameScene from my EndScene. The idea is the player will hit the "Restart" Button, reloading the GameScene allowing them to try again. Here is my code for the two scenes. Does anyone have a solution? Please give me the exact code I would need, this is my first time using Xcode, and I am new to using code. It would help me understand better how to deal with a problem like this in the future.

Here is the code for the GameScene:

var Highscore = Int()
var Score = Int()
var Player = SKSpriteNode (imageNamed: "good.png")
var ScoreLbl = UILabel()

override func didMoveToView(view: SKView) {

    var HighscoreDefault = NSUserDefaults.standardUserDefaults()
    if (HighscoreDefault.valueForKey("Highscore") != nil){

    Highscore = HighscoreDefault.valueForKey("Highscore") as! NSInteger
    }
    else {

    Highscore = 0
    }

physicsWorld.contactDelegate = self

self.scene?.backgroundColor = UIColor.blackColor()


self.addChild(SKEmitterNode(fileNamed: "Rain"))



Player.position = CGPointMake(self.size.width / 2, self.size.width / 6)
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCategory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
Player.physicsBody?.dynamic = false





var Timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("SpawnBullets"), userInfo: nil, repeats: true)

var EnemyTimer = NSTimer.scheduledTimerWithTimeInterval(0.8, target: self, selector: Selector("SpawnEnemies"), userInfo: nil, repeats: true)


self.addChild(Player)



ScoreLbl.text = "\(Score)"
ScoreLbl = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
ScoreLbl.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 1.0, alpha: 0.3)
ScoreLbl.textColor = UIColor.whiteColor()


self.view?.addSubview(ScoreLbl)

}

func didBeginContact(contact: SKPhysicsContact) {
    var firstBody : SKPhysicsBody = contact.bodyA
    var secondBody : SKPhysicsBody = contact.bodyB


    if ((firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.Bullet) || (firstBody.categoryBitMask == PhysicsCategory.Bullet) && (secondBody.categoryBitMask == PhysicsCategory.Enemy)){

    CollisionWithBullet(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)


    }
    else if ((firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.Player) || (firstBody.categoryBitMask == PhysicsCategory.Player) && (secondBody.categoryBitMask == PhysicsCategory.Enemy)){

    CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)


}

}

func CollisionWithBullet(Enemy: SKSpriteNode, Bullet: SKSpriteNode){
Enemy.removeFromParent()
Bullet.removeFromParent()
Score++

ScoreLbl.text = "\(Score)"

}

func CollisionWithPerson(Enemy: SKSpriteNode,Person: SKSpriteNode){
    var ScoreDefault = NSUserDefaults.standardUserDefaults()
    ScoreDefault.setValue(Score, forKey: "Score")
    ScoreDefault.synchronize()

    if (Score > Highscore){

    var HighscoreDefault = NSUserDefaults.standardUserDefaults()
    HighscoreDefault.setValue(Score, forKey: "Highscore")
}

Enemy.removeFromParent()
Person.removeFromParent()
self.view?.presentScene(EndScene())
ScoreLbl.removeFromSuperview()
self.removeAllActions()




}




func SpawnBullets() {
    var Bullet = SKSpriteNode (imageNamed: "bullet.png")
    Bullet.zPosition = -5

    Bullet.position = CGPointMake(Player.position.x, Player.position.y)
    let action = SKAction.moveToY(self.size.height + 30, duration: 1.0)
    let actionDone = SKAction.removeFromParent()
    Bullet.runAction(SKAction.sequence([action, actionDone]))

    Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
    Bullet.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
    Bullet.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
    Bullet.physicsBody?.affectedByGravity = false
    Bullet.physicsBody?.dynamic = false






self.addChild(Bullet)

}

func SpawnEnemies () {
    var Enemy = SKSpriteNode (imageNamed: "bad.png")
    var MinValue = self.size.width / 8
    var MaxValue = self.size.width - 15
    var Spawnpoint = UInt32(MaxValue - MinValue)
    Enemy.position = CGPointMake(CGFloat(arc4random_uniform(Spawnpoint)), self.size.height)
    Enemy.physicsBody = SKPhysicsBody(rectangleOfSize:Enemy.size)
    Enemy.physicsBody?.categoryBitMask = PhysicsCategory.Enemy
    Enemy.physicsBody?.contactTestBitMask = PhysicsCategory.Bullet
    Enemy.physicsBody?.affectedByGravity = false
    Enemy.physicsBody?.dynamic = true


let action = SKAction.moveToY(-70, duration: 2.5)
let ActionDone = SKAction.removeFromParent()
Enemy.runAction(SKAction.sequence([action, ActionDone]))

Enemy.runAction(SKAction.repeatActionForever(action))

self.addChild(Enemy)


}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {


    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        Player.position.x = location.x

    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch: AnyObject in touches  {
        let location = touch.locationInNode(self)

        Player.position.x = location.x
    }
}


override func update(currentTime: CFTimeInterval) {
     /* Called before each frame is rendered */
} 

And here is the code for the EndScene:

var RestartBtn : UIButton!
var Highscore : Int!
var ScoreLbl : UILabel!
var HighscoreLbl : UILabel!
override func didMoveToView(view: SKView) {
    scene?.backgroundColor = UIColor.blackColor()

    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)

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




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

    }


}

Thank you so much to anyone who can help! It means a lot! :)

0

There are 0 answers