ios - xcode : Thread 1: Signal SIGABART in class AppDelegate: UIResponder, UIApplicationDelegate(游戏应用程序)

标签 ios swift

在标记为重复之前,请考虑一下我已经搜索了网络和 StackOverflow,但没有找到针对此特定问题的任何解决方案(因为有关此错误的所有问题都针对应用程序,而不是游戏)。

我正在制作一个非常基本的太空射击游戏,我已经达到了一个阶段,我想让英雄(下面的代码)发射子弹;我创建了一个函数spawn_bullet来执行此操作。我写完这个函数并在函数中调用它之后:

覆盖 func update(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

最佳答案

它发生在这一行:

self.addChild(bullet)

这是因为 bullet 已经有一个父级。

要修复此问题,请在添加子项之前添加此行:

bullet.removeFromParent()

关于ios - xcode : Thread 1: Signal SIGABART in class AppDelegate: UIResponder, UIApplicationDelegate(游戏应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38385069/

相关文章:

ios - HLS 音频流在后台播放一段时间后无法恢复播放(AVPlayer 拒绝缓冲)

ios - 执行相互调用的方法。全部在后台

ios - 在 View 之间传递图像

swift - 如果 PromiseKit promise 已经在运行,那么返回待定 PromiseKit promise 的最佳 Swift 模式是什么?

ios - 动态提供数据时,AdBlock 在 iOS 中不起作用?

ios - 代码 : missing required module error

javascript - 类方法在未被调用的情况下被触发

ios - 内存警告和崩溃 ImageIO-jpeg-data

iphone - 唯一识别设备

ios - 如何使用 NSURlConnection 从 Url 下载图像以保存在手机缓存中