0

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)
    }
}
Flame
  • 1
  • 1
  • 2

3 Answers3

2

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)
    }
}
drewag
  • 93,393
  • 28
  • 139
  • 128
  • @zneak, I think the wrong part of that was "throw a", I am still used to exceptions :). I updated it to just say "it will assert" – drewag Jul 25 '14 at 00:01
  • @zneak See [the docs on assertions](https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_438). – ahruss Jul 25 '14 at 00:02
  • @zneak I think "assert" is right: the framework assertions deliberately throw a bad instruction at the processor to provoke the exception – Matt Gibson Jul 25 '14 at 17:12
  • I tried that. Then I got this problem: Conditional downcast from 'SKView!' to 'SKView' always succeeds. Im really not that a good coder on Xcode yet but thats what I got when I tried it. please help me get rid of this bug couse I really don't like it. – Flame Jul 29 '14 at 16:11
  • @Flame what class are you implementing this in? It seems that SKView is already of the type SKView (as an implicitly unwrapped optional). In that case, there is no reason to cast it at all and your code is probably crashing because `self.view` is nil (it isn't connected in interface builder most likely). – drewag Jul 29 '14 at 23:42
  • Im making this in PlayScene.swift. I'm using SpriteKit. How can i connect it in interface builder if it isn't in it?. And if I take away that line and replace skView with SKView or else it says: use of unresolved identifier skView, i just get a bunch of other problems in all the lines under exept the 2nd last line. (the one with .AspectFill) SKView.ignoresSiblingOrder = true gives me this error: 'SKView.Type' does not have a member named 'ignoresSiblingOrder'. same in the line under that exempt 'bounds' instead of 'ignoresSiblingOrder'. in the last line if gives me this error: – Flame Jul 30 '14 at 10:00
  • 'GameScene' is not convertible to 'SKView'. i didnt have enough place for it so i made this new comment – Flame Jul 30 '14 at 10:01
  • @Flame, sorry I don't know enough about how your project is setup to say exactly why it is `nil` or how you should correct it. First you should validate in the debugger if it is indeed `nil`, and then you should explore why it is `nil`. Is it never set to a value? Is something setting it back to `nil`? etc. – drewag Jul 30 '14 at 15:08
  • Thanks but the problem is just that I don't know how. – Flame Jul 30 '14 at 15:39
0

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!
0
if let skView = self.view as? SKView! {                                                          
    skView.ignoresSiblingOrder = true
    scene.size = skView.bounds.size
    scene.scaleMode = .AspectFill
    skView.presentScene(scene)
}

should work

digiwand
  • 1,258
  • 1
  • 12
  • 18