-2

I'm trying to make it so that my sprite receives impulse and velocity when a touch is made somewhere on the screen, but I get the error code:

fatal error: unexpectedly found nil while unwrapping an Optional value

It is forcing me to put a "!" in front of "physicsbody", and when I do that it crashes. If I put a "?" the impulse and velocity just won't work.

func setuprocket() {

    let rocket0 = SKTexture(imageNamed: "0zero.png")
    let rocket1 = SKTexture(imageNamed: "1one.png")
    let rocket2 = SKTexture(imageNamed: "2two.png")
    let rocket3 = SKTexture(imageNamed: "3three.png")
    let rocket4 = SKTexture(imageNamed: "4four.png")
    let rocket5 = SKTexture(imageNamed: "5five")



    let animateRocket = SKAction.sequence([
        SKAction.waitForDuration(0.0, withRange: 1.0),
        SKAction.animateWithTextures([rocket0,
            rocket1,
            rocket2,
            rocket3,
            rocket4,
            rocket5],
            timePerFrame: 0.1)])


    let rocketAnim = SKAction.repeatActionForever(animateRocket)



    let rocket = SKSpriteNode(texture: rocket1)

    rocket.runAction(rocketAnim)
    rocket.setScale(0.5)
    rocket.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.5)

    rocket.physicsBody = SKPhysicsBody(circleOfRadius: rocket.size.height)
    rocket.physicsBody?.dynamic = true
    rocket.physicsBody?.allowsRotation = false
    //bitmask
    rocket.physicsBody?.contactTestBitMask = asteroidCategory
    rocket.physicsBody?.collisionBitMask = asteroidCategory
    self.addChild(rocket)

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)


        rocket.physicsBody!.applyImpulse(CGVectorMake(0, 30))
        rocket.physicsBody!.velocity = CGVectorMake(0, 300)
        println(location)

        //if start.containsPoint(location) {
         //   self.physicsWorld.gravity = CGVectorMake(0.0, -3.0)
         //   start.removeFromParent()
       // }

        }

    }
    }

Everything is defined correctly, just the "!" crashes the game.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jacob
  • 91
  • 1
  • 7
  • 1
    This is pretty simple. It just means that your `physicsBody` is nil. Can you show how you're going about setting that property? – Mick MacCallum May 25 '15 at 17:44
  • i added the setup to the question at the bottom – Jacob May 25 '15 at 17:52
  • Alright, and what method are you setting up your physics body in? Is it somewhere that doesn't get called until after touches began? – Mick MacCallum May 25 '15 at 17:55
  • Its being setup in a function that creates my entire rocket. – Jacob May 25 '15 at 17:56
  • Can you show that function as well. It's possible that the problem isn't your physics body but your rocket if rocket is an implicitly unwrapped optional. – Mick MacCallum May 25 '15 at 17:57
  • You've update your title to fixed, this is not the correct way to mark a question as resolved on Stackoverflow. If you've found an answer that would be useful to future users then add the answer to Your Answer and click Post Your Answer. If it won't be useful for others you might want to delete your question. – Dijkgraaf May 26 '15 at 01:01

1 Answers1

0

I took out let rocket = SKSpriteNode(texture: rocket1) and put it at the very top beneath class. I made sure that this was called at the very top of my project rather than within a function.

Jacob
  • 91
  • 1
  • 7