当我按下按钮时,我的游戏崩溃(终止时未捕获的 NSException 类型的异常)



我正在用Xcode 9.0(测试版(制作我的游戏 基本上,我试图在结束场景中获取一个按钮,以便在单击时显示游戏场景,但是每当我单击该按钮时,游戏就会崩溃,并在控制台中显示以下内容:terminating with uncaught exception of type NSException.

有人可以帮我吗!?

下面是我使用的代码。

import SpriteKit

类 结尾场景: SKScene {

var replayButton:SKSpriteNode!
override func didMove(to view: SKView) {
replayButton = self.childNode(withName: "replay") as! SKSpriteNode
replayButton.position = CGPoint(x: 0, y: 0)
replayButton.setScale(0.5)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self) {
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "replay" {
let transition = SKTransition.push(with: .right, duration: 1)
let gameScene = GameScene(size: self.size)
self.view?.presentScene(gameScene, transition: transition)
}
}
}

}

我的按钮代码

这是应用崩溃时调试控制台中的所有内容:

2017-07-26 16:54:35.697622+0200 面[49264:5441087]* 由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:"试图添加已经具有父级的 SKNode:名称:"(空(" 纹理:[ "侧面BG1" (420 x 660(] 位置:{0, 0} 比例:{1.00, 1.00} 大小:{2000, 2000} 锚点:{0.5, 0.5} 旋转:0.00' *第一次抛出调用堆栈: ( 0 核心基金会0x000000010ae65c7b异常预处理 + 171 1 利比亚博博杰克。A.迪利布 0x0000000106a80121 objc_exception_throw + 48 2 核心基金会 0x000000010aed7a45 +[NSException raise:format:] + 197 3 雪碧套件 0x00000001077b26a1 -[SKNode insertChild:atIndex:] + 162 4 精灵套件 0x00000001077b25de -[SKNode 添加儿童:] + 68 5面0x000000010614d796 _T05Sides9GameSceneC7didMoveySo6SKViewC2to_tF+ 7398 6面 0x000000010614da2c _T05Sides9GameSceneC7didMoveySo6SKViewC2to_tFTo + 60 7 精灵套件 0x0000000107779d2d -[SKScene _didMoveToView:] + 204 8 精灵套件 0x0000000107799e00 -[SKView presentScene:transition:] + 347 9面0x0000000106156b42 _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtF+ 1730 10 面 0x0000000106156dc8 _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtFTo + 104 11 精灵套件 0x0000000107798181 -[SKView touchesBegan:withEvent:] + 1130 12 UIKit 0x0000000107a5f998 -[UIWindow _sendTouchesForEvent:] + 2130 13 UIKit 0x0000000107a61360 -[UIWindow sendEvent:] + 4124 14 UIKit 0x0000000107a074d2 -[UIApplication sendEvent:] + 352 15 UIKit 0x0000000108306384 __dispatchPreprocessedEventFromEventQueue + 2809 16 UIKit 0x0000000108308eeb __handleEventQueueInternal + 5957 17 核心基金会 0x000000010ae093a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION+ 17 18 核心基金会 0x000000010adedc8f __CFRunLoopDoSources0 + 271 19 核心基金会 0x000000010aded23f __CFRunLoopRun + 1039 20 核心基金会 0x000000010adecbb9 CFRunLoopRunspecific + 409 21 图形服务 0x000000010fd4d9e2 GSEventRunModal + 62 22 UIKit 0x00000001079eacb2 UIApplicationMain + 159 23 面0x00000001061559d7主面 + 55 24 libdyld.dylib 0x000000010bee4b95 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (分行(

尝试更新 did移至以下内容

var replayButton: SKSpriteNode!
override func didMove(to view: SKView) {
if (self.childNode(withName: "//replay") as? SKSpriteNode) != nil {
replayButton = self.childNode(withName: "replay") as! SKSpriteNode
replayButton.position = CGPoint(x: 0, y: 0)
replayButton.setScale(0.5)
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "replay"{
let transition = SKTransition.push(with: .right, duration: 1)
let gameScene = GameScene(size: self.size)
self.view?.presentScene(gameScene, transition: transition)
}
}
}

我编辑了代码,检查这是否可以在没有崩溃的情况下工作,并对按钮进行操作。

没关系,我修好了!这是因为它添加了已经存在的节点,所以我确实将 FromParent 删除到我的所有对象(除了一个,它是敌人(并且它有效(但是当我返回时对象仍然太大,所以我在过渡到我的 EndScene 时确实写了这个:let endScene = EndScene(fileNamed: "EndScene") endScene!.size = self.size endScene!.scaleMode = self.scaleMode(。

并在过渡到我的结尾场景时写下了这个:let gameScene = GameScene(fileNamed: "GameScene") gameScene!.size = self.size gameScene!.scaleMode = self.scaleMode

(非常感谢@Sulthan!

相关内容

最新更新