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.