Posts

Post not yet marked as solved
1 Replies
1.6k Views
Hi,I just installed an Xcode update and now my game app freezes when I build it and has a debug error statement:Thread 1: EXC_BAD_ACCESS(code=1, address=0x48)on the AVAudioPlayer line:do {backgroundAudio = try AVAudioPlayer(contentsOf: audioNSURL as URL) }Here is the complete code in GameViewController that used to work fine:Import SpriteKitImport GameplayKitImport AVFoundationclass GameViewController: UIViewController {var backgroundAudio = AVAudioPlayer()override func viewDidLoad() {super.viewDidLoad()let filePath = Bundle.main.path(forResource: “MySong”, ofType: “wav”)let audioNSURL = NSURL(fileURLWithPath: filePath!)do {backgroundAudio = try AVAudioPlayer(contentsOf: audioNSURL as URL) }catch {return print(“Cannot Find The Audio”)}backgroundAudio.numberOfLoops = -1backgroundAudio.volume = 0.7background.play()}What has happened and how can I fix this?
Posted
by Zwolf71.
Last updated
.
Post not yet marked as solved
0 Replies
384 Views
Hi,I have hardcoded a MainMenu scene to 2042x1536. This works well across different devices where a certain perimeter is cropped in the iPhone simulators. But when I create a subclass (Welcome), it doesn’t inherit the aspectFill behaviour from the MainMenu superclass for the iPhone simulators (the iPads are fine), and stretches the background image to fill the SKView.Why is this so? Is the anything I can do so that the Welcome class background image maintains the same hardcode behaviour from its MainMenu super class?Thanks in advance,Here are my codes:import UIKitimport SpriteKitimport GameplayKitclass GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let scene = MainMenu(size: CGSize(width: 2048, height: 1536)) let skView = self.view as! SKView skView.showsFPS = true skView.showsNodeCount = true skView.ignoresSiblingOrder = true scene.scaleMode = SKSceneScaleMode.aspectFill skView.presentScene(scene) } override var shouldAutorotate: Bool { return true } override var prefersStatusBarHidden: Bool { return true }}class MainMenu: SKScene { let background = SKSpriteNode(imageNamed: “bg01”)required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(size: CGSize) { super.init(size: size) background.size.width = frame.size.width background.position = CGPoint(x: size.width/2, y: size.height/2) background.anchorPoint = CGPoint(x: 0.5, y: 0.5) background.zPosition = 1 addChild(background) } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in (touches) { let location = touch.location(in: self) let nodeTouched = atPoint(location) if nodeTouched.name == "welcome" { self.view?.presentScene(Welcome(size: self.size)) } } } class Welcome: MainMenu { override func didMove(to view: SKView) { } } // Welcome_Class closure} // MainMenu_SuperClass closure
Posted
by Zwolf71.
Last updated
.
Post marked as solved
4 Replies
1.4k Views
Hi,I am practising subclassing. I have a superclass GameScene and subclass Scene02. I created a scoring that is called in touches began. But when the app transitions to Scene02, the score returns to zero rather than inheriting the score update that is updated in the superclass GameScene init(). How can I make the score in Scene02 class to continue the last score (int 5) from the superclass?Thanks in advanceimport SpriteKitimport GameplayKitclass GameScene: SKScene { var score = 0 let scoreLabel = SKLabelNode(fontNamed: "Arial") required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(size: CGSize) { super.init(size: size) 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) updateScore() } override func didMove(to view: SKView) { self.anchorPoint = .zero self.backgroundColor = SKColor.green } func updateScore() { scoreLabel.text = "score: \(score)" } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { score += 1 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) { scoreLabel.text = "score: \(score)" }}import SpriteKitimport GameplayKitclass Scene02: GameScene { override func didMove(to view: SKView) { self.anchorPoint = .zero self.backgroundColor = SKColor.red updateScore() }}
Posted
by Zwolf71.
Last updated
.
Post not yet marked as solved
3 Replies
1.1k Views
Hi,Can an enum contain a list of classes?I have a superclass Activity: SKScene { }, which has sub-private class Activity01: Activity { }, sub-private class Activity02: Activity { } and a sub-private class Activity03: Activity { }.Each sub-private class contains a timed activity. When the timer runs out before the user solves the activity, a GameOver scene (class GameOver: Activity { }) pops up (with text “Bad luck try again”). I am trying to create an enum in the GameOver class to be triggered in touches began to return the user to their last activity but I cannot solve this one. When tapped it always returns to Activity01. Is there a way to return to Activity02 or Activity03 if they were the last activity?Here is my attempt:…} // Activity03 sub-private class closure class GameOver: Activity { enum classes { case Activity01 case Activity02 case Activity03 } var firstClass = classes.Activity01 var secondClass = classes.Activity02 var thirdClass = classes.Activity03 let restartGameText = SKLabelNode(fontNamed: "AvenirNext-HeavyItalic") init(size: CGSize, won:Bool) { super.init(size: size) startGameText.removeFromParent() restartGameText.text = "Bad luck try again" restartGameText.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center restartGameText.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center restartGameText.fontSize = 60 restartGameText.fontColor = SKColor(red: 230/255, green: 60/255, blue: 70/255, alpha: 1) restartGameText.position = CGPoint(x: scene!.size.width/2.0, y: scene!.size.height/2.0) restartGameText.zPosition = 5 addChild(restartGameText) let pulseAction = SKAction.sequence([SKAction.fadeAlpha(to: 1, duration: 1), SKAction.fadeAlpha(to: 0.7, duration: 1)]) restartGameText.run(SKAction.repeatForever(pulseAction)) } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if firstClass == classes.Activity01 { let transition = SKTransition.fade(withDuration: 2.0) let activity01 = Activity01(size: size) view?.presentScene(activity01, transition: transition) } else if secondClass == classes.Activity02 { let transition = SKTransition.fade(withDuration: 2.0) let activity02 = Activity02(size: size) view?.presentScene(activity02, transition: transition) } else if thirdClass == classes.Activity03 { let transition = SKTransition.fade(withDuration: 2.0) let activity03 = Activity03(size: size) view?.presentScene(activity03, transition: transition) } } } //GameOver class closure} // Activity Superclass closureThank you in advanceh
Posted
by Zwolf71.
Last updated
.