2016-07-14 55 views
0

在標記爲重複之前,請考慮我搜索了網絡和StackOverflow,但我沒有找到任何解決方案,因爲這個具體問題(因爲關於這個錯誤的所有問題都針對應用程序,而不是遊戲)。xcode:線程1:信號SIGABART在類AppDelegate:UIResponder,UIApplicationDelegate(遊戲應用程序)

我正在製作一款非常基本的太空射擊遊戲,並且我到達了一個我想讓英雄(代碼如下)拍攝子彈的舞臺;我創建了一個函數spawn_bullet來做到這一點。之後,我寫了這個功能,並把它稱爲在功能:

覆蓋FUNC更新(currentTime的:CFTimeInterval),

我得到的錯誤:線程1:信號SIGABRT。圖片如下所示

enter image description here 我相信錯誤有事情做與spawn_bullet功能的執行,但我不知道它在哪裏,爲什麼它引起的,如何解決它。任何人有一個想法來解決它,如果可能提供足夠的細節,爲什麼它會導致?

謝謝!

注意 英語不是我的第一語言(這就是爲什麼它是壞的)

下面我提供GameScene.swift用我的代碼:

import SpriteKit 

class GameScene: SKScene { 

    var hero = SKSpriteNode(imageNamed: "hero") 
    var bullet = SKSpriteNode(imageNamed: "bullet") 
    var enemy = SKSpriteNode(imageNamed: "enemy") 
    var init_bullet_position = CGFloat(0) 
    var bullet_velocityY = CGFloat(5) 

    override func didMoveToView(view: SKView) { 
     /* Setup your scene here */ 
     self.backgroundColor = UIColor(red: 0.7, green: 0.7, blue: 0.8, alpha: 1) 

     self.hero.anchorPoint = CGPointMake(0.5, 0) 
     self.hero.size.height = 50 
     self.hero.size.width = 30 
     self.hero.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinX(self.frame) + self.hero.size.height) 

     self.bullet.anchorPoint = CGPointMake(0.5, 0) 
     self.bullet.size.height = 30 
     self.bullet.size.width = 15 
     self.bullet.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinX(self.frame) + self.hero.size.height + self.hero.size.height/4) 

     self.init_bullet_position = CGFloat(CGRectGetMinX(self.frame) + self.hero.size.height + self.hero.size.height/4) 

     self.hero.zPosition = 1; //1 = front 
     self.addChild(bullet) 
     self.addChild(hero) 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     /* Called when a touch begins */ 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode!(self) 
      hero.position.x = location.x 
      if self.bullet.position.y == init_bullet_position{ 
       self.bullet.position.x = location.x 
      } 
     } 
    } 

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     /* Called when a touch begins */ 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode!(self) 
      self.hero.position.x = location.x 
      if self.bullet.position.y == init_bullet_position{ 
       self.bullet.position.x = location.x 
      } 
     } 
    } 

    func spawn_bullets(){ 
     self.bullet.anchorPoint = CGPointMake(0.5, 0) 
     self.bullet.size.height = 30 
     self.bullet.size.width = 15 
     self.bullet.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinX(self.frame) + self.hero.size.height + self.hero.size.height/4) 
     self.addChild(bullet) 

     if self.bullet.position.y < self.frame.size.height{ 
      self.bullet.position.y += self.bullet_velocityY 
     } 
    } 

    override func update(currentTime: CFTimeInterval) { 
     /* Called before each frame is rendered */ 
     spawn_bullets() 
    } 
} 

這裏是異常消息:

2016-07-14 23:07:54.564 Space shooter[14391:191912] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'bullet' (400 x 600)] position:{160, 62.5} scale:{1.00, 1.00} size:{15, 30} anchor:{0.5, 0} rotation:0.00' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010ec4fd85 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000111299deb objc_exception_throw + 48 
    2 CoreFoundation      0x000000010ec4fcbd +[NSException raise:format:] + 205 
    3 SpriteKit       0x000000010fb74903 -[SKNode insertChild:atIndex:] + 162 
    4 SpriteKit       0x000000010fb74840 -[SKNode addChild:] + 68 
    5 Space shooter      0x000000010ea62d23 _TFC13Space_shooter9GameScene13spawn_bulletsfT_T_ + 995 
    6 Space shooter      0x000000010ea62ef4 _TFC13Space_shooter9GameScene6updatefSdT_ + 36 
    7 Space shooter      0x000000010ea62f2c _TToFC13Space_shooter9GameScene6updatefSdT_ + 44 
    8 SpriteKit       0x000000010fb48072 -[SKScene _update:] + 312 
    9 SpriteKit       0x000000010fb6503a -[SKView _update:] + 932 
    10 SpriteKit       0x000000010fb6221a __59-[SKView _renderSynchronouslyForTime:preRender:postRender:]_block_invoke + 170 
    11 SpriteKit       0x000000010fb620e8 -[SKView _renderSynchronouslyForTime:preRender:postRender:] + 235 
    12 SpriteKit       0x000000010fb64ac2 -[SKView layoutSubviews] + 85 
    13 UIKit        0x000000010fdd8980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 
    14 QuartzCore       0x00000001151d7c00 -[CALayer layoutSublayers] + 146 
    15 QuartzCore       0x00000001151cc08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
    16 QuartzCore       0x00000001151cbf0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    17 QuartzCore       0x00000001151c03c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 
    18 QuartzCore       0x00000001151ee086 _ZN2CA11Transaction6commitEv + 486 
    19 QuartzCore       0x00000001151ee7f8 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 
    20 CoreFoundation      0x000000010eb74c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    21 CoreFoundation      0x000000010eb74ba7 __CFRunLoopDoObservers + 391 
    22 CoreFoundation      0x000000010eb6a11c CFRunLoopRunSpecific + 524 
    23 UIKit        0x000000010fd18f21 -[UIApplication _run] + 402 
    24 UIKit        0x000000010fd1df09 UIApplicationMain + 171 
    25 Space shooter      0x000000010ea64e82 main + 114 
    26 libdyld.dylib      0x0000000111dc792d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
+1

編輯你的問題,並補充說,得到了打印到控制檯 – dan

+0

也請出示你的代碼的堆棧跟蹤+異常消息:

要解決它,你添加子前加入這一行。 –

+0

對不起,我們缺少的部分。我成功更新了我的問題。 – GeorGios

回答

0

回答陳述的問題:

這是你的錯誤:

Attemped to add a SKNode which already has a parent

的問題是在你spawn_bullets()功能。你實際上並沒有在這個函數中實例化新的項目符號。你只是更新自己的子彈實例的屬性,然後嘗試添加它作爲你的場景的孩子。第二次調用這個函數時,它會崩潰,因爲子彈已經是場景的孩子。

相反,你應該創建你的子彈節點的新實例。

關於創造獨特的子彈根本問題:

這是在其本身的整體獨立的主題。我假設你只想在特定的時間發射子彈,比如當你點擊某物時,而不是從update()函數中產生子彈,在每個幀被渲染之前調用子彈,我會從tap方法或其他方法調用spawn_bullets()相互作用。其次,你會想創建你的項目符號的新實例,而不是更新你場景中的變量。這些可以添加,動畫,然後刪除。最後,請查看SKAction課程,以實際製作動畫(並最終刪除)子彈。例如:

let newBullet = SKSpriteNode(imageNamed: "bullet") 
newBullet.position = CGPoint(x: 10, y: 10) 
self.addChild(newBullet) 

let moveAction = SKAction.moveTo(CGPoint(x: 50, y: 10), duration: 0.4) 
let removeAction = SKAction.removeFromParent() 
newBullet.runAction(SKAction.sequence([moveAction, removeAction])) { 
    // Bullet is done and removed. 
} 

你可以做更多的事情與SKAction比在這裏顯示。您可以播放音頻文件,遵循路徑,甚至可以簡單地運行一個塊。

+0

嗯,我正在試圖讓英雄射擊子彈。有沒有辦法做到這一點? – GeorGios

+0

*(驚訝...)*這會導致*中止*信號?哇。 (請注意,道爾頓,我**不會和你爭執!) –

0

它發生在這條線:

self.addChild(bullet) 

這是因爲bullet已經有母。

bullet.removeFromParent() 
相關問題