SpriteKit 暂停恢复错误

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()
}
}

当触摸在继续按钮上结束时,我遇到线程 1: Signal SIGABRT 错误。我发现的是,如果我在触摸结束时声明精灵(继续按钮),该按钮不会被删除,但是,当我在函数外声明继续按钮精灵时,继续按钮是 removed.The 问题是有时该应用程序将崩溃并显示线程 1 错误。关于如何停止崩溃有什么想法吗?

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")
...}

这是声明按钮的地方(在函数之外)

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

第三行代码出现错误。 这是电脑告诉我的。

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

原因是因为您将 upButton 作为子节点添加到某个节点两次。

很可能是您的 touchesEnded 方法中的 self.addChild(upButton) 行导致了问题。此时,验证 upButton.parent 是否在 nil 中。如果不是,则您的代码中存在逻辑错误。