SKEmitterNode 没有从父节点中删除自己? [英] SKEmitterNode isn't removing itself from parent?

查看:15
本文介绍了SKEmitterNode 没有从父节点中删除自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 SKEmitterNode 添加为我的 mainScene 的 childNode 并且超出预期它会在 particleLifetime 结束时被删除,描述为

当我运行它时,发射器不会从屏幕上移除.

如果您需要更多信息,我不知道要添加什么,请寻求任何帮助,我们将不胜感激.

解决方案

particleLifetime 确定粒子的平均寿命,以秒为单位.这不会影响从父级删除 SKEmitterNode.

numOfParticlesToEmit 指的是粒子编辑器的Particles 区域中的Maximum 字段,它决定了发射器在停止之前应该发射的粒子数.这也不影响从父级删除 SKEmitterNode .另请注意,您在此字段中设置了 0,这将启用无限发射.

所以,如果你想在发射器发射完毕后从父节点中移除节点,你可以设置发射的粒子数(Particles 区域中名为 Maximum 的字段在编辑器中)并运行一个 SKAction 序列,它将:

  • 启动发射器
  • 等待一段时间
  • 并从父级移除发射器(此时发射器应该完成发射)

下面是一个简单的例子,向您展示如何使用 SKAction 序列来做到这一点:

类 GameScene: SKScene {让发射器:SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(NSBundle.mainBundle().pathForResource("MyParticle", ofType: "sks")!) 作为 SKEmitterNode覆盖函数 didMoveToView(视图:SKView){self.backgroundColor = SKColor.blackColor()}func addEmitter(位置:CGPoint){var EmitterToAdd = Emitter.copy() as SKEmitterNodeemitterToAdd.position = 位置让 addEmitterAction = SKAction.runBlock({self.addChild(emitterToAdd)})var 发射器持续时间 = CGFloat(emitter.numParticlesToEmit) * 发射器.particleLifetime让等待 = SKAction.waitForDuration(NSTimeInterval(emitterDuration))let remove = SKAction.runBlock({emitterToAdd.removeFromParent(); println("Emitter removed")})让序列 = SKAction.sequence([addEmitterAction, 等待, 删除])self.runAction(序列)}覆盖 func touchesBegan(touches: NSSet, withEvent 事件: UIEvent) {让触摸:AnyObject?= touches.anyObject()让 location = touch?.locationInNode(self)self.addEmitter(位置!)}}

这是结果(注意在发射完成后节点的计数是如何变化的):

希望对你有帮助

对于那些对如何制作与上面视频类似的效果感兴趣的人,请尝试以下方法:

重点是使用颜色渐变,并选择添加作为混合模式.

这是 .sks 文件的 Dropbox 链接:Effect.sks

I added SKEmitterNode as a childNode of my mainScene and than expected that It would be removed when particleLifetime ends, described like apple docs.

Added emitters like this;

    var emitterPath : String = NSBundle.mainBundle().pathForResource("ShipFire", ofType: "sks")!
    var emitter : SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(emitterPath) as! SKEmitterNode

    emitter.position = position
    emitter.particleLifetime = 0.1;

    self.addChild(emitter)

My SKEmitterNode properties like image below

When I run it emitters aren't removed from screen.

I don't know what to add more if you need more information please ask any help would be appreciated thanks.

解决方案

particleLifetime determines the average lifetime of a particle, in seconds. That doesn't affect on removal of SKEmitterNode from parent.

numOfParticlesToEmit which refers to Maximum field in Particles area of Particle Editor determines the number of particles that emitter should emit before stopping. That doesn't affect on removal of SKEmitterNode from parent too. Also note that you've set 0 in this field which will enable infinitely emitting.

So, if you want to remove node from parent when emitter is done with emitting, you can set the number of particles to emit (field called Maximum in Particles area inside editor) and run an SKAction sequence which will:

  • start an emitter
  • wait for some duration of time
  • and remove the emitter from parent (at this point emitter should finish with emitting)

Here is an simple example to show you how to do this with SKAction sequence:

class GameScene: SKScene {


    let emitter : SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(NSBundle.mainBundle().pathForResource("MyParticle", ofType: "sks")!) as SKEmitterNode
    override func didMoveToView(view: SKView) {

        self.backgroundColor = SKColor.blackColor()


    }

    func addEmitter(position:CGPoint){

        var emitterToAdd   = emitter.copy() as SKEmitterNode

        emitterToAdd.position = position

        let addEmitterAction = SKAction.runBlock({self.addChild(emitterToAdd)})

        var emitterDuration = CGFloat(emitter.numParticlesToEmit) * emitter.particleLifetime

        let wait = SKAction.waitForDuration(NSTimeInterval(emitterDuration))

        let remove = SKAction.runBlock({emitterToAdd.removeFromParent(); println("Emitter removed")})

        let sequence = SKAction.sequence([addEmitterAction, wait, remove])

        self.runAction(sequence)


    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {


        let touch: AnyObject? = touches.anyObject()

        let location = touch?.locationInNode(self)

        self.addEmitter(location!)



    }


}

And here is the result (note how node's count is changing after emitting is done) :

Hope this helps

EDIT:

For those interested in how to make similar effect like from the video above, try with something like this:

The point is to use Color Ramp, and to choose Add for a blend mode.

Here is the dropbox link to the .sks file : Effect.sks

这篇关于SKEmitterNode 没有从父节点中删除自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆