when I type this line of code into swift I get an error. Please help!

when I type this into xcode(6)

let ball = childNodeWithName(BallCategoryName) as! SKSpriteNode

I get this Error Message when I try to run it

Thread 1:EXC_BAD_INTRSUCTION (code=EXC_1386_INVOP, subcode=0x0)



here is the full lump of code


import SpriteKit

let BallCategoryName = "ball"

let PaddleCategoryName = "paddle"

let BlockCategoryName = "block"

let BlockNodeCategoryName = "blockNode"

class GameScene: SKScene {

override func didMoveToView(view: SKView) {

super.didMoveToView(view)

let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

borderBody.friction = 0

self.physicsBody = borderBody

physicsWorld.gravity = CGVectorMake(0, 0)

let ball = childNodeWithName (BallCategoryName) as! SKSpriteNode

ball.physicsBody!.applyImpulse(CGVectorMake(10, -10))


Thanks a lot, I am using swift in xcode 6 by the way, I am only new to this so It may be something really obvious to anyone else!


William Gregory

Hello. How is the SKSpriteNode named "ball" created? Does it exist at the time the crashing code executes? What does childNodeWithName return if no such node is found? Does it return nil? According to the docs 'childNodeWithName' returns an SKNode? which is either the node or nil if no such node was found. If it is returning nil, then that could be the problem. However, I'm going out on a bit of a limb here because I have no evidence for some of my guesses.


I think I would double check that the ball is being created correctly before I arrive in didMoveToView. I'd also be inclined to break that childNodeWithName(BallCategoryName) as! SKSpriteNode into multiple pieces: the first one to find the ball, then make sure it is not nil, then cast it, but cast it in such a way that if it fails to cast it doesn't crash. I think the 'if let...' construction would help a bit with both the nil checking and the casting.

let ball = childNodeWithName (BallCategoryName) as! SKSpriteNode


The reason this line is causing the EXC_BAD_INTRSUCTION is probably that you are using as! which causes an exception if the conversion is not possible.


The childNodeWithName(name: String) -> SKNode? function is returning an optional SKNode, not an SKSpriteNode; it can't be cast as a subclass.


If you need an SKSpriteNode, you will need to create one by calling SKSprinteNode() with an appropriate initializer, and then you can add it to your other scene/node using addChild(node: SKNode).

I changed the

let ball = childNodeWithName (BallCategoryName) as SKSpriteNode

into

let ball = childNodeWithName (BallCategoryName) as SKNode!

if thats what you meant, but it comes out with this error:

Thread 1: breakpoint 1.1

Sorry if this isnt what you meant but I found it a little comfusing, probably because of my lack of knowledge :s


Thanks so much for the respose!


William Gregory

Hi DressTheMonkey


Thanks for the response, but I am slightly Confused by your awnser as i dont know much about swift


could you explain it in simpler terms?


thanks a lot


William Gregory

As others have said, it appears that your SKSpriteNode that is named "ball" has not been created when you are asking for it. If you meant to create it here than the line should be changed to "var ball = SKSpriteNode(name of image)". You can then name the ball with "ball.name = BallCategoryName".

Add the ball SKSpriteNode to you scene using the addChild(ball) function and let the physics to the work.

The SpriteKit Programming Guide has the details about setting up your scene and adding the nodes.

Gregstus, It is really difficult to give non-trivial help on a forum. Forums are really best for short answers to specific questions. Now, even though you have a specific question (why does this line crash?) and the answer turns out also to be specific (because the node doesn't exist or can't be cast to that type), it does require the context of both how SpriteKit works and how Swift works to understand why that answer answers that question! Unfortunately, I (and I assume the others trying to help) just haven't got the time/knowledge/ability/desire (delete as appropriate) to provide the necessary background and context in Swift and SpriteKit to explain how and why that answer answers that question. In order for our answers to make sense, that context has to exist.


The way to get that context is by finding some write-ups and tutorials and example apps and studying them. Apple documentation and Sample Apps are one source, and there are many many tutorials on the web. Although I have quite a bit of programming experience, I'm just learning Swift (like most people, I have only been at it a little over a year) and I'm a complete novice with SpriteKit (I'm working on my first little test app right now). And every question I have had has been answered by strategic Googling and careful study of and experimentation with the results I get back.


It is very gratifying and exciting when something works, but it can be a slow process (well, at least it is for me because this kind of programming is way outside of my normal 'zone' which is more like business apps).


All the best,

doug --dd

when I type this line of code into swift I get an error. Please help!
 
 
Q