Search code examples
flashactionscript-3particles

Particle system on AS3, using flash, problem copying the particle element


im developing a very simple particle system in AS3, i have the particle (a movieclip) and the particle behavior, but now i need a good way to duplicate it n number of times and changing the only value that determine the system behavior, the width, from 10 to 100 px.

This is the code:

//some declarations
var blur:BlurFilter = new BlurFilter();
var filterArray:Array = new Array(blur);
import fl.transitions.Tween;
import fl.transitions.easing.*;

//the only input value, from 10 to 100
par.width=100;
//the equations that define the behavior.
par.alpha=.0088*par.width+.98;
par.height=par.width;
blur.blurX = .75*par.width-.55;
blur.blurY = blur.blurX;
blur.quality = 1;
par.filters = filterArray;
//the movement of the particle
var myTween:Tween = new Tween(par, "y", Strong.easeOut, par.y, stage.stageHeight+2*par.height, -.2*par.width+22, true); 

So, as you can see, par is the instance name for the particle, well, i need to duplicate it changing the .width value and eventually the .x value too. Any ideas? Thanks!


Solution

  • This is what OOP (Object Oriented Programming) is all about, and Flash is a great example.

    package  {
    
        import flash.filters.BlurFilter;
        import fl.transitions.Tween;
        import fl.transitions.easing.*;
        import flash.display.MovieClip;
        import flash.events.Event;
    
        public class Particle extends MovieClip {
    
            public function Particle() {
                // constructor code
                //some declarations
                this.graphics.beginFill(0, 1);
                this.graphics.drawCircle(0, 0, 50);
                var blur:BlurFilter = new BlurFilter();
                var filterArray:Array = new Array(blur);
                //the only input value, from 10 to 100
                this.width = Math.round(Math.random() * 90) + 10;
                //the equations that define the behavior.
                this.alpha = .0088 * this.width + .98;
                this.height = this.width;
                blur.blurX = .75 * this.width - .55;
                blur.blurY = blur.blurX;
                blur.quality = 1;
                this.filters = filterArray;
                this.addEventListener(Event.ADDED_TO_STAGE, __tweenMe);
            } 
    
    
            private function __tweenMe($evt:Event):void {
                //the movement of the particle
                var myTween:Tween = new Tween(this, "y", Strong.easeOut, this.y, stage.stageHeight+2*this.height, -.2*this.width+22, true); 
            }
    
        }
    
    }
    

    and then in your DocumentClass you could do something like this:

    package  {
    
        import flash.display.MovieClip;
    
        public class BaseClass extends MovieClip {
    
            public function BaseClass() {
            var par:Particle;
                for ( var i:int = 0; i < 100; i++) {
                    par = new Particle();
                    addChild(par);
                }       
            }   
        }    
    }
    

    EDIT

    Here you go http://d.pr/ycUh. Let me know if you have questions about what is going on. I added some random x and y values for the starting positions of your particles.