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); } } } } } |
nice! but is starling supports mobile game development? since i heard stage3D is not yet supported in air…
Hello Lee. There’s some code in your updateParticles function that’s now showing.
@Rahmat it will be available on mobile soon!
@Lic thanks I updated the video and included the code in the post.
I think there’s a bug there, when splicing out of an array like that you need to loop backwards, in this case some indexes will get missed / ignored.
I guess it would get picked up in the next frame anyways…
Should be:
for(var i:int = explosions.length -1; i >= 0; i–){
…
explosions.splice(i, 1);
…
}
No offence Lee, but “soon”? What does that mean?
Soon in the dictionary of Adobe:
1. once we can sell you a new suite
2. an undefined period of time, once the unicorns rule the world
I am getting tired of this crap, sorry to say. I own a suite for 2k USD and I am still waiting for Stage3D to be added through an update. This is ridiculous, free products have better updates. This is how Adobe values it customers?
Of course I know this has nothing to do with you, but maybe the time has come to push a bit the correct people.
nice thank you !
Hi Lee,
thanks a lot for pointing me at this painful omission! I just added a “complete” event to the repository, which should greatly simplify this task.
Daniel
Ahhhh fresh air ! That´s my good and old Lee back to the Flash Player ^^
Exactly, Shawn.
I tought the same thing all the video. I usually would do some like
if(ex.numParticles == 0)
{
ex.stop();
explosions.splice(i, 1);
Starling.juggler.remove(ex);
removeChild(ex, true);
i–;
}
But your code looks great too.
Thanks.
do you know when? because that will be kick ass!
Were these stars loaded from a separated bitmap?
When I first saw this class, I wondered if it was made to automatically remove from parent when the particle effect ends. What do you think about this approuch?
Nice tip indeed.
@Ramon yes if you look where I show the Explosion class I embed both the particle PEX file and the texture star bitmap.
Good one!
What’s even better is that you have a cat!!1
And yeah, how about that AIR3.2 SDK, where can we sign up for beta?
Hi Lee. Long time no-look-at-your-website… Great stuff though. I’ve missed out a lot I see!
Curious to know if you’d consider doing a comparable tutorial using ND2D which appears to have some nice aspects to it and is preferred by some people I gather?
http://www.nulldesign.de/category/experiments/nd2d/
Thanks for the continuing excellent work, though.
Hi Lee
Great tip – and very easy to implement. One little snag though: I can’t seem to create even the most basic explosion like effect in the onebyonedesign particle effect creator. I’ve been tweaking everything but nothing resembling an explosion – starting in one point and expanding outwards. Any help – from anyone – would be greatly appreciated.
This is awesome bro, thanks heaps for posting!!