gpt4 book ai didi

ios - Spritekit 游戏崩溃(因为 SKLightNode 线程问题?)

转载 作者:行者123 更新时间:2023-11-28 08:39:26 25 4
gpt4 key购买 nike

我正在尝试为我的 children 制作一款简单的游戏。我正在尝试实现灯光和发射器节点。然而,游戏在碰撞时崩溃,这应该会导致 gameOverScene。奇怪的是,如果我添加一个刹车点,代码可以正常完成。这是代码:

   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
touch = touches.first!
turnOnLightNode(touch.locationInNode(self))
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
touch = touches.first!
turnOnLightNode(touch.locationInNode(self))
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
removeLightNode()
}
func removeLightNode(){
var nodeToRemove = self.childNodeWithName("light")
while(nodeToRemove != nil && endOfSceneCollision == false) {
nodeToRemove?.removeFromParent()
nodeToRemove = self.childNodeWithName("light")
}
nodeToRemove = self.childNodeWithName("touchEmitterNode")
while(nodeToRemove != nil && endOfSceneCollision == false) {
nodeToRemove?.removeFromParent()
nodeToRemove = self.childNodeWithName("touchEmitterNode")
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
removeLightNode()
}
func turnOnLightNode(point: CGPoint){

removeLightNode()

light.name = "light"
light.categoryBitMask = 3
light.position = point
light.zPosition = 19.0
light.falloff = 0.5
light.enabled = true
light.lightColor = UIColor(red: 161/255, green: 218/255, blue: 237/255, alpha: 0.5)
light.shadowColor = UIColor(red: 161/255, green: 218/255, blue: 237/255, alpha: 0.5)
light.ambientColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 0.3)

addChild(light)

touchEmitter!.name = "touchEmitterNode"
touchEmitter!.position = point
touchEmitter!.zPosition = 100//gameFieldParticlesZPosition
addChild(touchEmitter!)
}

func didBeginContact(contact: SKPhysicsContact) {
var ballBody: SKPhysicsBody?
var lineBody: SKPhysicsBody?
var collidedBallNode: SKSpriteNode?

if contact.bodyA.categoryBitMask == 2 && contact.bodyB.categoryBitMask == 1 {
print("didBeginContactAB")

lineBody = contact.bodyA
ballBody = contact.bodyB
collidedBallNode = contact.bodyB.node as? SKSpriteNode
}
if contact.bodyA.categoryBitMask == 1 && contact.bodyB.categoryBitMask == 2 {
print("didBeginContactBA")

lineBody = contact.bodyB
ballBody = contact.bodyA
collidedBallNode = contact.bodyA.node as? SKSpriteNode
}
if contact.bodyA.categoryBitMask == 1 && contact.bodyB.categoryBitMask == 4 {
//audioController.playSound(electricBounceSound, volume: 1.0)
}
if contact.bodyA.categoryBitMask == 4 && contact.bodyB.categoryBitMask == 1 {
//audioController.playSound(electricBounceSound, volume: 1.0)
}
if contact.bodyA.categoryBitMask == 1 && contact.bodyB.categoryBitMask == 1 {
//audioController.playSound(electricNoiseSound, volume: 1.0)
}

if(collidedBallNode != nil){
gotoGameOverScene(collidedBallNode!)

}
}
func gotoGameOverScene(explodingBallNode: SKSpriteNode){
print("step 1") //IF BREAKPOINT HERE, ALL EXECUTES OK
self.runAction(SKAction.waitForDuration(2.5), completion: {
print("step 2")
let gameOverScene = GameOverScene(size: self.size)
print("step 3")
self.view?.presentScene(gameOverScene, transition: reveal)
print("step 4")
})
}

结果是这样的:

    didBeginContactAB
step 1
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKLightNode> name:'light' position:{296.99994, 191.49995} scale:{1.00, 1.00} accumulatedFrame:{{297, 191.5}, {0, 0}}'
*** First throw call stack:
(
0 CoreFoundation 0x01384a14 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00b5de02 objc_exception_throw + 50
2 CoreFoundation 0x0138493d +[NSException raise:format:] + 141

但是如果我在以下行之前放置调试器断点:

print("step 1")

代码成功执行。一定是线程/同步问题,但这胜过我对 Spritekit/Swift 的专业知识。有人可以帮我解决这个问题吗?

最佳答案

啊……我明白了。 removeLightNode 函数中的一段愚蠢代码。这导致碰撞后移除不起作用:while(nodeToRemove != nil && endOfSceneCollision == false)

关于ios - Spritekit 游戏崩溃(因为 SKLightNode 线程问题?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36772742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com