EXC_BAD_INSTRUCTION bug - ios

I have gotten this problem several times in Xcode 6. the thing is that I got a ball running and when he hits a block he dies. he can jump though. but the very first block always crashes. like if I just roll normally like not in the air and hit the first block it just freezes and closes.
It gives me this error:
Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)
Line number 2 and 7 have a green background (counting the space).
This is the error line:
func didBeginContact(contact:SKPhysicsContact) {
died()
}
func died() {
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
let skView = self.view as SKView
skView.ignoresSiblingOrder = true
scene.size = skView.bounds.size
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}

EXC_BAD_INSTRUCTION implies that there was an assert somewhere in your code. The only line of the code you provided that can throw an assert is:
let skView = self.view as SKView
It will assert if self.view cannot be cast to an SKView. It seems as if self.view is not actually an SKView.
To be sure, you can do an optional cast like you did with scene:
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
if let skView = self.view as? SKView {
skView.ignoresSiblingOrder = true
scene.size = skView.bounds.size
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}

I Believe I have found a fix for your problem.
change let skView = self.view as SKView to
if let skView = self.view as SKView!

if let skView = self.view as? SKView! {
skView.ignoresSiblingOrder = true
scene.size = skView.bounds.size
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
should work

Related

gameViewController background color cannot be changed

The App running under spriteKit. Days before, the gameScene was loaded once the gameViewController was loaded. Today, I change its loading action that the gameScene is loaded only when the Start button is clicked, it means the gameScene background color is invisible, I need to set a background color for gameViewController. It's a very very simple task being done many times
before, but it sticks on light gray(image below).
Can't gameViewController background color be changed under spriteKit?
Must I change gameScene background color instead?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .green
// .......
}
#IBAciton func loadGameView() {
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
// if let scene = SKScene(fileNamed: "GameScene") {
if let scene = GameScene(fileNamed: "GameScene") {
scene.viewController = self
scene.size = self.view.frame.size
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = false
view.showsFPS = false
view.showsNodeCount = false
view.showsDrawCount = false
}
}
if let scene = GameScene(fileNamed: "GameScene") {
scene.viewController = self
scene.size = self.view.frame.size
scene.scaleMode = .aspectFill
//ADD THIS
scene.color = .green
scene.colorBlendFactor = 1
// Present the scene
view.presentScene(scene)
}

Swift 2 Game Main Menu

I am trying to implement a main menu to my Spritekit game, but whenever I try to present the scene I get a blank gray screen. Here is the code right now, which presents the game itself and not a menu:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsDrawCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = SKSceneScaleMode.AspectFill
skView.presentScene(scene)
}
}
I'm new to this, and I wasn't sure what to do so I tried replacing GameScene with the menu scene, which gave me the gray screen. Any help will be appreciated.
Use this code to load a SKScene file that is created in code only and not in the Scene editor
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let skView = self.view as? SKView {
if skView.scene == nil {
let scene = MenuScene(size: skView.bounds.size)
skView.showsFPS = false
skView.showsNodeCount = true
skView.showsPhysics = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .aspectFill
skView.presentScene(scene)
}
}
}
and then in your MenuScene file you will need an init func
init(size: CGSize) {
super.init(size: size)
name = "menuScene"
}

(Swift SpriteKit) Only allow SKScene to present once

This code resides in the viewDidLoad function. The first time it is called it is fine. But then I present a subView on top of it from the same storyboard file and when 'let skView = gameView as! SKView' gets called again and throws a Fatal error: Found nil... How do I stop it from being called the other times? I can't think of any checks I could but in place.
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = gameView as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let skView = self.view as! SKView
if skView.scene == nil {
if let scene = GameScene(fileNamed:"GameScene") {
skView.presentScene(scene)
}
}
}

Problems with SpriteKit after upgrading to Swift 1.2

I've just upgraded to Swift 1.2 and I'm having trouble with my SpriteKit scene. My code below worked before the upgrade.
override func viewDidLoad() {
super.viewDidLoad()
// Configure the view.
let skView = self.view as! SKView
let scene:SKScene = GameScene.init(size: skView.bounds.size)
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
The problem I'm having is with the line
let scene:SKScene = GameScene.init(size: skView.bounds.size)
The compiler complains that it expects a member name or constructor call after the type name. When I follow the compiler's instructions I get into a circle of further errors and complaints from the compiler. Any ideas?
I copied your code into my Swift 2.1 project and it runs fine.
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Configure the view.
let skView = self.view as! SKView
let scene:SKScene = GameScene.init(size: skView.bounds.size)
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}

Nothing is displaying in Simulator with viewWillLayoutSubviews

Within my GameView Controller.swift file, I removed viewDidLoad and put in viewWillLayoutSubviews because I will be using the dimensions of the device in my calculations. However, hello world SKLabelNode no longer is displayed in the simulator. Any ideas on what I'm doing wrong?
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let skView = self.view as SKView
if skView.scene != nil{
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true
// Create and configure the scene
let scene = SKScene(size: skView.bounds.size)
scene.scaleMode = SKSceneScaleMode.AspectFill;
// Present Scene
skView.presentScene(scene)
}

Resources