我正在 Xcode 9.0(测试版)中制作游戏,基本上,我试图在结束场景中获得一个按钮,以便在单击该按钮时呈现游戏场景,但是每当我单击该按钮时,游戏就会崩溃,并在控制台中显示:由于未捕获的 NSException 类型异常而终止。
有人可以帮帮我吗?
下面是我使用的代码。
import SpriteKit
EndScene 类: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 Sides[49264:5441087] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“尝试添加已具有父级的 SKNode:名称” :'(null)' 纹理:[ 'SidesBG1' (420 x 660)] 位置:{0, 0} 比例:{1.00, 1.00} 大小:{2000, 2000} anchor :{0.5, 0.5} 旋转:0.00' * 首先抛出调用堆栈: ( 0 CoreFoundation 0x000000010ae65c7b 异常预处理 + 171 1 libobjc.A.dylib 0x0000000106a80121 objc_异常_抛出 + 48 2 CoreFoundation 0x000000010aed7a45 +[NSException 引发:格式:] + 197 3 SpriteKit 0x00000001077b26a1 -[SKNode insertChild:atIndex:] + 162 4 SpriteKit 0x00000001077b25de -[SKNode addChild:] + 68 5面0x000000010614d796_T05Sides9GameSceneC7didMoveySo6SKViewC2to_tF + 7398 6 面 0x000000010614da2c _T05Sides9GameSceneC7didMoveySo6SKViewC2to_tFTo + 60 7 SpriteKit 0x0000000107779d2d -[SKScene _didMoveToView:] + 204 8 SpriteKit 0x0000000107799e00 -[SKView PresentScene:transition:] + 347 9 边 0x0000000106156b42 _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtF + 1730 10 面 0x0000000106156dc8 _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtFTo + 104 11 SpriteKit 0x0000000107798181 -[SKView TouchsBegan:withEvent:] + 1130 12 UIKit 0x0000000107a5f998 - [UIWindow _sendTouchesForEvent:] + 2130 13 UIKit 0x0000000107a61360 - [UIWindow发送事件:] + 4124 14 UIKit 0x0000000107a074d2 - [UIApplication发送事件:] + 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 类型的未捕获异常终止 (lldb)
最佳答案
尝试将 didMove 更新为以下内容
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)
}
}
}
我编辑了代码,检查这是否可以正常工作而不会崩溃并在按钮上执行操作。
关于ios - 当我按下按钮时,我的游戏崩溃(因 NSException 类型的未捕获异常而终止),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45329681/