0 Replies - 775 Views - Last Post: 16 July 2017 - 05:34 AM Rate Topic: -----

#1 moeafg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-July 17

Help implementing saving highscore in Swift

Posted 16 July 2017 - 05:34 AM

Hi all,

I have followed a YouTube tutorial to creating an Swift game using SpriteKit. The tutorial fails to show to save the high scores when the user launches the app so i've been trying to implement this. I understand it requires UserDefaults but my code keeps crashing. I have // the code I have implemented without success.





 import Foundation 
 import SpriteKit  


  class GameMenu: SKScene{ 
    
     var startGame = SKLabelNode() 
   var bestScore = SKLabelNode() 
     var gameSettings = Settings.sharedInstance 
   
    //var higherScore = SKLabelNode() 
    //let higherScore = UserDefaults.standard.object(forKey: "higherScore") as! Int 
   
    
    override func didMove(to view: SKView) { 
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5) 
        startGame = self.childNode(withName: "startGame") as! SKLabelNode  
         bestScore = self.childNode(withName: "bestScore") as! SKLabelNode 
        bestScore.text = "Best : \(gameSettings.highScore)" 
        
       // higherScore = self.childNode(withName: "higherScore") as! SKLabelNode
       // higherScore.text = "High Score : \(UserDefaults.standard.set(bestScore, forKey: "higherScore"))"
       // UserDefaults.standard.synchronize()
        
        //let highScore = UserDefaults.object(forKey: "highScore")) as! Int
        
       // UserDefaults.standard.set(bestScore, forKey: "higherScore")
       // UserDefaults.standard.synchronize()
        
        //let highScore = UserDefaults.standard.object(forKey: "highScore") as! Int
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let touchLocation = touch.location(in: self)
            if atPoint(touchLocation).name == "startGame"{
                let gameScene = SKScene(fileNamed: "GameScene")!
                gameScene.scaleMode = .aspectFill
                view?.presentScene(gameScene, transition: SKTransition.doorsOpenHorizontal(withDuration: TimeInterval(2)))
   
        }
    }
    
}

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1