主题1:SpriteKit游戏中的信号SIGABRT错误

问题描述:

这个问题与我之前提到的有关我在SpriteKit中制作的Galaga游戏的问题有关。 (Cannot assign value of type '[SKNode]' to type 'SKSpriteNode!')我只是继续我与我的新问题一起去那里的谈话。主题1:SpriteKit游戏中的信号SIGABRT错误

我有一个线程1信号:当我尝试按下我的游戏中的开火按钮时,应用程序委托中的SIGABRT错误(开火按钮是一个SKSpriteNode。)有时它会触发,但只有当我按下非常特定的按钮时位置。否则,它会给我SIGABRT错误。这可能是从代码进来我touchesBegan功能,

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

for touch in touches { 
    let location = (touch as UITouch).location(in: self) 
    let nodes = self.nodes(at: location) 
    for node in nodes { 
     if node.name == "FireButton" { 
      shoot() 
     } else { 
      let touchLocation = touch.location(in: self) 
      spaceship.position.x = touchLocation.x 
     } 
    } 
    } 
} 

fireLaser功能,

func fireLaser(laser: SKNode, toDestination destination: CGPoint, withDuration duration: CFTimeInterval, andSoundFileName soundName: String) { 
    let laserAction = SKAction.sequence([ 
    SKAction.move(to: destination, duration: duration), 
    SKAction.wait(forDuration: 3.0/60.0), 
    SKAction.removeFromParent() 
    ]) 

addChild(laser) 
laser.run(SKAction.group([laserAction])) 
} 

shoot功能。

func shoot() { 
    self.laser.position = CGPoint(x: self.spaceship.position.x, y: 
    self.spaceship.position.y + self.spaceship.frame.height - 
    self.laser.frame.height/2) 
    self.addChild(self.laser) 
    let laserDestination = CGPoint(x: self.spaceship.position.x, y: 
     self.frame.height + self.laser.frame.height/2) 
     self.fireLaser(laser: self.laser, toDestination: laserDestination, withDuration: 1.0, andSoundFileName: "laser sound effect.mp3") 
} 

下面是我在看到错误时看到的屏幕截图。 [错误截图] [1]

而且,这里的打印出控制台时,我得到了错误的消息,

2017-06-12 17:02:06.100 Galaga Copy[790:23281] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'Laser' (27 x 150)] position:{-5.8244380950927734, -411.53384399414062} scale:{1.00, 1.00} size:{13.5, 75} anchor:{0.5, 0.5} rotation:0.00' 

***第一掷调用堆栈: ( 0的CoreFoundation 0x0000000105cfcb0b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010261f141 objc_exception_throw + 48 2的CoreFoundation 0x0000000105d65625 + [NSException提高:格式:] + 197 3 SpriteKit 0x0000000103218c95 - [SKNode insertChild:atIndex:] + 162 4 SpriteKit 0x0000000103218bd2 - [SKNode的addChild:] + 68 5 Galaga游戏复印0x0000000102036601 _TFC11Galaga_Copy9GameScene5shootfT_T_ + 1089 6 Galaga游戏复印0x0000000102036f57 _TFC11Galaga_Copy9GameScene12touchesBeganfTGVs3SetCSo7UITouch_4withGSqCSo7UIEvent__T_ + 1431 7 Galaga游戏复印0x0000000102037436 _TToFC11Galaga_Copy9GameScene12touchesBeganfTGVs3SetCSo7UITouch_4withGSqCSo7UIEvent__T_ + 102 8 SpriteKit 0x00000001032007c4 - [SKView的touchesBegan:withEvent:方法] + 1130 9的UIKit 0x000000010347154b - [一个UIWindow _sendTouchesForEvent:] + 2036 10的UIKit 0x0000000103472f00 - [一个UIWindow的SendEvent:] + 4114 11的UIKit 0x000000010341fa84 - [UIApplication的的SendEvent:] + 352 12 0×00的UIKit 00000103c035d4 __dispatchPreprocessedEventFromEventQueue + 2926 13的UIKit 0x0000000103bfb532 __handleEventQueue + 1122 14的CoreFoundation 0x0000000105ca2c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 15的CoreFoundation 0x0000000105c880cf __CFRunLoopDoSources0 + 527 16的CoreFoundation 0x0000000105c875ff __CFRunLoopRun + 911 17的CoreFoundation 0x0000000105c87016 CFRunLoopRunSpecific + 406个 18 GraphicsServices 0x000000010a159a24 GSEventRunModal + 62 19的UIKit 0x0000000103402134 UIApplicationMain + 159 20 Galaga Copy 0x0000000102039b07 main + 55 21 libdyld.dylib 0x0000000106c9c65d start + 1 ) libC++ abi。dylib:以NSException类型的未捕获异常终止 (lldb)

对不起,这一切都没有重击,但我不知道如何使它正常显示。任何人都可以帮助我吗?

+0

请你也可以添加在屏幕右下角打印的内容的细节。 – JohnV

+0

您正在多次添加相同的节点“激光”。您必须每次创建一个新的激光实例。 –

+0

我的另一个想法。有没有人知道如何让船只不断点火而不需要用户按下火灾按钮?这样做会更容易,因为我不需要使按钮复杂化很多。 – Shock9616

self.addChild(self.laser)

可能是你的问题。重新分配节点而不将它们从以前的父母中移除可能会导致此问题。

我建议.removeFromParent先看看是否有效。我注意到你把它放在fire()函数中,但在块运行之前,你可能会调用shoot()

+0

是的,再次添加激光是问题所在。应用终止时的消息是'试图添加一个已经有父节点的SKNode: name:'(null)'texture:['Laser'(27 x 150)]等等等等。是激光,我猜你正在使用激光节点。 – JohnV