我有一个简单的类函数,可以在许多不同的场合添加SKEmitterNode。
发生异常时,我无法重现步骤。它很少发生,而且很随机。我可以无错误地调用此方法500次,或者在某些情况下1或2次调用后可能发生错误,等等。
问题/例外行是:
root?.addChild(sparks)
下面是方法和堆栈跟踪。我不知道该如何调试。我尝试了许多不同的方法,但没有成功。
有任何想法吗 ?
class func setSimpleSparksEffect(root:SKNode?, color:UIColor, position:CGPoint)
{
if CGPointEqualToPoint(position, CGPointZero)
{
return
}
let sparks = SKEmitterNode(fileNamed: "SimpleSparks")
sparks.alpha = GameObjectsDefaultAlpha
sparks.particleColorSequence = nil
sparks.particleColorBlendFactor = 1.0
sparks.particleColor = color
sparks.position = position
sparks.zPosition = SparksElementsZPosition
root?.addChild(sparks)
sparks.runAction(SKAction.waitForDuration(NSTimeInterval(EmmiterSimpleShortDuration)), completion: { () -> Void in
sparks.runAction(SKAction.fadeOutWithDuration(NSTimeInterval(FactorSparksFadeOutDuration)), completion: { () -> Void in
sparks.removeAllActions()
sparks.removeAllChildren()
sparks.removeFromParent()
})
})
}
和堆栈跟踪:
Thread : Crashed: com.apple.main-thread
0 libc++abi.dylib 0x0000000199944ce4 __dynamic_cast + 52
1 SpriteKit 0x000000018ab63f58 __15-[SKNode scene]_block_invoke + 60
2 SpriteKit 0x000000018ab63f58 __15-[SKNode scene]_block_invoke + 60
3 SpriteKit 0x000000018ab28038 SKCNode::walkUp(void (SKCNode*, bool*) block_pointer, bool) + 76
4 SpriteKit 0x000000018ab63eac -[SKNode scene] + 132
5 SpriteKit 0x000000018ab643ac -[SKNode insertChild:atIndex:] + 356
6 SpriteKit 0x000000018ab64224 -[SKNode addChild:] + 76
7 DodgeMaster 0x00000001000f1d80 static DodgieCommon.setSimpleSparksEffect(SKNode?, color : UIColor, position : CGPoint) -> () (DodgieCommon.swift:275)
8 DodgeMaster 0x00000001000d2bc4 GameLevel.(gameLogicGoalerHitmeContact(GameLevel) -> (NSNotification) -> ()).(closure #1) (GameLevel.swift:502)
9 DodgeMaster 0x0000000100125018 static Helper.(runAsyncOnMain(Helper.Type) -> (() -> ()) -> ()).(closure #1) (Helper.swift:270)
10 DodgeMaster 0x00000001000b41c4 thunk (Pointoser.swift)
11 libdispatch.dylib 0x000000019aa917b0 _dispatch_call_block_and_release + 24
12 libdispatch.dylib 0x000000019aa91770 _dispatch_client_callout + 16
13 libdispatch.dylib 0x000000019aa96e20 _dispatch_main_queue_callback_4CF + 1844
14 CoreFoundation 0x000000018574c258 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
15 CoreFoundation 0x000000018574a0c0 __CFRunLoopRun + 1628
16 CoreFoundation 0x0000000185678dc0 CFRunLoopRunSpecific + 384
17 GraphicsServices 0x00000001907cc088 GSEventRunModal + 180
18 UIKit 0x000000018ad52f60 UIApplicationMain + 204
19 DodgeMaster 0x000000010011de24 main (AppDelegate.swift:22)
20 libdyld.dylib 0x000000019aac28b8 start + 4
最佳答案
这里的另一个人有一个similar problem,我想您可能也会遇到...我已针对您的帖子量身定制了我的回复。
let sparksFile: String = NSBundle.mainBundle().pathForResource("SimpleSparks", ofType: "sks")!
let sparks = NSKeyedUnarchiver.unarchiveObjectWithFile(explosionFile) as! SKEmitterNode
sparks.alpha = GameObjectsDefaultAlpha
sparks.particleColorSequence = nil
sparks.particleColorBlendFactor = 1.0
sparks.particleColor = color
sparks.position = position
sparks.zPosition = SparksElementsZPosition
self.root?.addChild(sparks)
关于ios - SKNode addChild发生奇怪随机崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32693021/