线程1:在SpriteKit游戏

时间:2017-06-13 00:15:01

标签: ios swift3 sprite-kit xcode8

这个问题与我之前关于我在SpriteKit中制作的Galaga游戏的问题有关。  (Cannot assign value of type '[SKNode]' to type 'SKSpriteNode!')我只是继续谈论我带着新问题去那里。

我有一个线程1信号:当我尝试在我的游戏中按下开火按钮时,App Delegate中的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 raise:format:] + 197     3 SpriteKit 0x0000000103218c95 - [SKNode insertChild:atIndex:] + 162     4 SpriteKit 0x0000000103218bd2 - [SKNode addChild:] + 68     5 Galaga Copy 0x0000000102036601 _TFC11Galaga_Copy9GameScene5shootfT_T_ + 1089     6 Galaga Copy 0x0000000102036f57 _TFC11Galaga_Copy9GameScene12touchesBeganfTGVs3SetCSo7UITouch_4withGSqCSo7UIEvent__T_ + 1431     7 Galaga Copy 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 UIKit 0x0000000103c035d4 __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)

很抱歉这一切都没有了,但我无法弄清楚如何让它正确显示。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

self.addChild(self.laser)

可能是您的问题。重新分配节点而不将其从先前的父节点中删除可能会导致此问题。

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