Posts

Post not yet marked as solved
1 Replies
I did some research and found the solution courtesy to Ray Wenderlich and Co. by creating an non-optional var and func with string parameter:Import SpriteKitImport GameplayKitImport AVFoundationclass GameViewController: UIViewController {var backgroundAudio = AVAudioPlayer!override func viewDidLoad() {super.viewDidLoad()playBackgroundAudio(audioname: “MySong.wav”)}//vDLfunc playBackgroundAudio(audioname: String) {let urlPath = Bundle.main.url(forResource: audioname, withExtension: nil)guard let url = urlPath else {print(“Could not find file: \(audioname)”)return}do {try backgroundAudio = AVAudioPlayer(contentsOf: url)backgroundAudio.numberOfLoops = -1backgroundAudio.volume = 0.7background.play()}catch {print(“Cannot Find The Audio”)return}}//func}//ClassGVCBut I still don’t understand why the old code stopped working when Xcode updated.
Post marked as solved
4 Replies
Ok Thank you. Appreciate your help.
Post marked as solved
4 Replies
That’s awesome OOPer, thank you. So simple.I am trying studying different design patterns and trying to achieve the same thing using a protocol. I’m just stabbing in the dark this time. Below is my clumsy attempt and of course Xcode is yelling. How can I make this work? Thanks in advance again.import SpriteKitimport GameplayKitprotocol KeepScore { var score: Int {get set} var scoreLabel: String {get set}}extension KeepScore { mutating func updateScore(count: Int) { score += 1 let scoreLabel = SKLabelNode(fontNamed: "Arial") scoreLabel.text = "score: \(score)" } func currentScore() -> Int { return score } }class GameScene002: SKScene, KeepScore { var scoreLabel = "score: Score" var score = 0 override func didMove(to view: SKView) { self.anchorPoint = .zero self.backgroundColor = SKColor.green let scoreLabel = SKLabelNode(fontNamed: "Arial") scoreLabel.fontSize = 35 scoreLabel.text = "Score: Score" scoreLabel.fontColor = SKColor.black scoreLabel.position = CGPoint(x: scene!.size.width/2.0, y: scene!.size.height/2.0) scoreLabel.zPosition = 5 self.addChild(scoreLabel) } func updateScore() { score += 1 let scoreLabel = SKLabelNode(fontNamed: "Arial") scoreLabel.text = "score: \(score)" } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { updateScore() if score == 5 { let transition = SKTransition.fade(withDuration: 2.0) let scene02 = Scene02(size: size) view?.presentScene(scene02, transition: transition) } } override func update(_ currentTime: TimeInterval) { updateScore() }}import SpriteKitimport GameplayKitclass Scene002: SKScene, KeepScore { var scoreLabel: String = "Score: Score" var score = 0 var currentScore = 0 override func didMove(to view: SKView) { self.anchorPoint = .zero self.backgroundColor = SKColor.red currentScore() } }