B.Toaster
B.Toaster

Reputation: 169

SpriteKit Pause Resume Error

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if continueButton.contains(touchLocation) {
    scene?.view?.isPaused = false
    pauseMenu.removeFromParent()
    self.addChild(pauseButton)
    self.addChild(upButton)
    continueButton.removeFromParent()
}
}

I am having a Thread 1: Signal SIGABRT error when touches ended on the continue button. Something I have found is that if I declare the sprite (continue button) in touches ended the button does not get removed, however, when I declare the continue button sprite outside the function the continue button is removed.The problem is that sometimes the app will just crash showing the thread 1 error. Any ideas on how I can stop the crashing?

class GameScene: SKScene {
var mainturret = SKSpriteNode(imageNamed: "Main Turret")
var pauseButton = SKSpriteNode(imageNamed: "Pause Simbol")
var angleToShoot = 0
var touchIsOn = 0
var pointTwoSecondInterval = 0
let pauseMenu = SKSpriteNode(imageNamed: "Paused")
var zombieSpawningTimer = Timer()
var upButton = SKSpriteNode(imageNamed: "Up Button")
let downButton = SKSpriteNode(imageNamed: "Down Button")
let popUpMenu = SKSpriteNode(imageNamed: "TurretSpot")
var continueButton = SKSpriteNode(imageNamed: "Continue")
...}

This is where the buttons are declared (outside the functions)

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

Error occurs on the third line of code. This is what the computer tells me.

2017-06-30 07:32:26.053429-0600 Survive the Night[4635:1030550] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' texture:[ 'Up Button' (200 x 200)] position:{0, -254} scale:{0.57, 0.57} size:{56.800003051757812, 56.800003051757812} anchor:{0.5, 0.5} rotation:0.00' * First throw call stack: (0x188da6fd8 0x187808538 0x188da6f20 0x1984f1738 0x1984f1664 0x100073d40 0x100074908 0x1984d7050 0x18ef0a46c 0x18ef05804 0x18eed6418 0x18f6cff64 0x18f6ca6c0 0x18f6caaec 0x188d55424 0x188d54d94 0x188d529a0 0x188c82d94 0x18a6ec074 0x18ef3b130 0x100078c5c 0x187c9159c) libc++abi.dylib: terminating with uncaught exception of type NSException

Upvotes: 1

Views: 55

Answers (1)

Benzi
Benzi

Reputation: 2459

The reason is because that you are adding upButton twice as a child to some node.

Most likely, the the line self.addChild(upButton) in your touchesEnded method is causing the issue. At that point, verify if upButton.parent in nil. If not, you have a logical error in your code.

Upvotes: 2

Related Questions