touchesBegan error ; everywhere I touch on the screen the game crashes

309 views Asked by At

So, I'm making a game for the iOS and it's literally all done except now my touchesBegan function is causing errors. This is how my function is laid out right now:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    var doo = SKAction.removeFromParent()
    /* Called when a touch begins */
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if GameOver{
            var gameScene = EasyScene(size: self.size)
            gameScene.scaleMode = SKSceneScaleMode.AspectFill
            self.scene!.view?.presentScene(gameScene)
        }
        else{
            if red.containsPoint(location){ // This is Line A
                //takeOut(red)
                red.removeFromParent()
                score++
                scoreNode.text = String(score)
                print("-hi")
            }
            if red1.containsPoint(location){
                red1.removeFromParent()
                score++
                scoreNode.text = String(score)
                //takeOut(red1)
                print("-hel")
            }
            if red2.containsPoint(location){
                red2.removeFromParent()
                score++
                scoreNode.text = String(score)
                //takeOut(red2)
                print("-hell")
            }
            if red3.containsPoint(location){
                red3.removeFromParent()
                score++
                scoreNode.text = String(score)
                //takeOut(red3)
                print("-hello")
            }
            if red4.containsPoint(location){
                red4.removeFromParent()
                //takeOut(red4)
                score++
                scoreNode.text = String(score)
                print("-hellow")
            }
            if red5.containsPoint(location){
                red5.removeFromParent()
                score++
                scoreNode.text = String(score)
                // takeOut(red5)
                print("-hellowor")
            }
            if blue.containsPoint(location){
                GameOver = true
            }
            if blue1.containsPoint(location){
                GameOver = true
            }
            if blue2.containsPoint(location){
                GameOver = true
            }
        }
    }
}

the issue is that when GameOver is false: anything I touch on-screen will cause a crash. If I touch the screen (no spriteNode in particular) it points to the line labeled "This is Line A" however if I tap on a node that is actually on the screen, the game will crash and point towards the spriteNode that I pressed saying the same thing:

fatal error: unexpected nil while unwrapping optional value

also when GameOver == true toouchesBegan works fine

1

There are 1 answers

0
CoolMAn On

The reason my game was crashing was because I had declared what my sprite was in a function that I had programmed to be executed 5 seconds after initial start of my app. However I find that the game runs properly now that my red sprite is declared in didMoveToView