2017-06-13 76 views
2

這個問題與我之前提到的有關我在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

回答

1

self.addChild(self.laser)

可能是你的問題。重新分配節點而不將它們從以前的父母中移除可能會導致此問題。

我建議.removeFromParent先看看是否有效。我注意到你把它放在fire()函數中,但在塊運行之前,你可能會調用shoot()

+0

是的,再次添加激光是問題所在。應用終止時的消息是'試圖添加一個已經有父節點的SKNode: name:'(null)'texture:['Laser'(27 x 150)]等等等等。是激光,我猜你正在使用激光節點。 – JohnV