在标记为重复之前,请考虑我已经搜索了网络和StackOverflow,但我没有找到针对此特定问题的任何解决方案(因为关于此错误的所有问题都是针对应用而非游戏)。
我正在制作一个非常基本的太空射击游戏,我到了一个舞台,我想让英雄(下面的代码)射击子弹;我创建了一个函数spawn_bullet来执行此操作。在我写完这个函数并在函数中调用它之后:
覆盖func update(currentTime:CFTimeInterval),
我收到错误:主题1:信号SIGABRT 。图片如下所示
我相信错误与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
答案 0 :(得分:0)
回答上述问题:
这是你的错误:
尝试添加已有父级的SKNode
问题出在您的spawn_bullets()
功能中。你实际上并没有在这个函数中实例化新的项目符号。您只是更新自我的bullet实例的属性,然后尝试将其添加为场景的子项。第二次调用此函数时,它会崩溃,因为项目符号已经是场景的子项。
相反,您应该创建项目符号节点的新实例。
关于创建独特项目符号的根本问题:
这本身就是一个完全独立的话题。我假设您只想在特定时间点击子弹,例如当你点击某些东西时,所以不是从update()
函数产生子弹,而是在渲染每个帧之前调用它,我会调用{{1}来自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.
}
可以比此处显示的更多。您可以播放音频文件,关注路径,甚至只是运行一个块。
答案 1 :(得分:0)
它出现在这一行:
self.addChild(bullet)
这是因为bullet
已有父母。
要修复它,请在添加子项之前添加此行:
bullet.removeFromParent()