Update: I am happy to report that Daniel has now updated the particle extension with a complete event. See my much-simplified code directly below. Thanks Daniel!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package
{
    import starling.core.Starling;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.events.Touch;
    import starling.events.TouchEvent;
    import starling.events.TouchPhase;
   
    public class Main extends Sprite
    {              
        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
       
        private function init(event:Event):void
        {          
            stage.addEventListener(TouchEvent.TOUCH, onTouch);
        }

        private function onTouch(event:TouchEvent):void
        {
            var t:Touch = event.getTouch(stage);
           
            if(t.phase == TouchPhase.ENDED)
            {
                var ex:Explosion = new Explosion();
                ex.addEventListener("complete", removeParticle);
                Starling.juggler.add(ex);
                ex.emitterX = t.globalX;
                ex.emitterY = t.globalY;
                addChild(ex);              
                ex.start(0.1);
            }
        }
       
        private function removeParticle(event:Event):void
        {
            var ex:Explosion = event.target as Explosion;          
            ex.stop();
            Starling.juggler.remove(ex);
            removeChild(ex, true);
        }
    }
}

In this quick tip I show how to track and clean up Starling particle effects. There will hopefully be some built-in support for this soon but for now this is how I’m handling it. I prefer to not update the framework itself but that could be another approach. I also included the code below the video.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package
{
    import starling.core.Starling;
    import starling.display.Quad;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.events.Touch;
    import starling.events.TouchEvent;
    import starling.events.TouchPhase;
   
    public class Main extends Sprite
    {      
        private var explosions:Vector.<Explosion>;
       
        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
       
        private function init(event:Event):void
        {
            explosions = new Vector.<Explosion>();
           
            stage.addEventListener(TouchEvent.TOUCH, onTouch);
           
            // main game loop
            addEventListener(Event.ENTER_FRAME, loop);
        }

        private function onTouch(event:TouchEvent):void
        {
            var t:Touch = event.getTouch(stage);
           
            if(t.phase == TouchPhase.ENDED)
            {
                var ex:Explosion = new Explosion();
                explosions.push(ex);
                Starling.juggler.add(ex);
                ex.emitterX = t.globalX;
                ex.emitterY = t.globalY;
                addChild(ex);              
                ex.start(0.1);
                ex.advanceTime(1);
            }
        }
       
        private function loop(event:Event):void
        {
            updateParticles();
        }
       
        private function updateParticles():void
        {
            var ex:Explosion;
           
            for(var i:int=0; i<explosions.length; i++)
            {
                ex = explosions[i];
                if(ex.numParticles == 0)
                {
                    ex.stop();
                    explosions.splice(i, 1);
                    Starling.juggler.remove(ex);
                    removeChild(ex, true);
                }
            }
        }
    }
}