User Login





Register
Forget Password

Hostings with very low prices

Hosting Plans Starting at 1$/Month

BaydHost

Powered By

  • AhmBay
  • How to make particle explosions - ActionScript Webmaster Tips, Knowledge Base Webmaster Tools

    Home > ActionScript > How to make particle explosions
    Category: ActionScript
    Written by: Admin
    Date: 2008-11-10
    Rating: 0   Puan:0 | Katılımcı:0 | Voted : 0 times
    Hit: 18
      

    s a bit of a treat, I’m going to show you how to make particle “explosions” that will come in handy in games, visulisations or other projects.



    Here is what we’ll be making: (click to create an explosion.)


    First of all, create a new Flash document. The width and height you choose doesn’t really matter.




    Press ctrl+F8 to create a new Movie Clip. Enter Particle for the name.



    What we want to do here is go to the advanced options and tick the box that says Export for ActionScript. This will mean that we can link the graphics of this movie clip to the class that we will create later on.



    In the textbox labeled Class, enter Particle.






    What the create MovieClip window should look like



    After entering these settings, press ok. Create a square that is around 3px wide and 3px height. The colour doesn’t matter as we will change this dynamically later on.



    Go back to the main timeline.



    Now we have our Particle MovieClip set up. At the moment, this doesn’t do anything at all.



    Let’s add some ActionScript.



    Press File -> New and create a new ActionScript File (*.as)






    Save this file as Particle.as



    Make sure that when you save this file, it is in the same folder as your main .fla file.



    The ActionScript that we will put into this Particle.as file will control each particle that is on the screen.



    Actionscript [Show Styled Code]:
    class Particle extends MovieClip{
    var vx,vy;
    var timeToLast,startTimer;
    var myColor,rgbValue;
    function onLoad(){
    var angle = Math.random()*360;
    vx = Math.cos(angle)*Math.random()*20+5;
    vy = Math.sin(angle)*Math.random()*20+5;
    timeToLast = Math.random()*500 + 1000;
    startTimer = getTimer();
    myColor = new Color(this);
    myColor.setRGB(rgbValue);
    }
    function onEnterFrame(){
    this._x += vx;
    this._y += vy;
    this._alpha -=2;
    if(getTimer() - startTimer >= timeToLast || this._alpha == 0){
    removeMovieClip(this);
    }
    }
    }


    Actionscript [Show Plain Code]:


    class Particle extends MovieClip{


    var vx,vy;


    var timeToLast,startTimer;


    var myColor,rgbValue;


    function onLoad(){


    var angle = Math.random()*360;


    vx = Math.cos(angle)*Math.random()*20+5;


    vy = Math.sin(angle)*Math.random()*20+5;


    timeToLast = Math.random()*500 + 1000;


    startTimer = getTimer();


    myColor = new Color(this);


    myColor.setRGB(rgbValue);


    }


    function onEnterFrame(){


    this._x += vx;


    this._y += vy;


    this._alpha -=2;


    if(getTimer() - startTimer >= timeToLast || this._alpha == 0){


    removeMovieClip(this);


    }


    }


    }




    It may look confusing, but I’ll take you through it and explain it.



    Actionscript [Show Styled Code]:
    class Particle extends MovieClip{


    Actionscript [Show Plain Code]:


    class Particle extends MovieClip{




    This just sets Particle up as a class that extends the MovieClip class. This means that the class Particle will inherit features from the MovieClip class.



    Actionscript [Show Styled Code]:
    var vx,vy;
    var timeToLast,startTimer;
    var myColor,rgbValue;


    Actionscript [Show Plain Code]:


    var vx,vy;


    var timeToLast,startTimer;


    var myColor,rgbValue;




    Here we’re just setting up variables for our Particle. vx, vy (x and y velocities), timeTolast,startTimer (how long the particle lasts and what time the particle is first created at), myColor,rgbValue (for setting our Particle’s colour.)



    Actionscript [Show Styled Code]:
    function onLoad(){
    var angle = Math.random()*360;
    vx = Math.cos(angle)*Math.random()*20+5;
    vy = Math.sin(angle)*Math.random()*20+5;
    timeToLast = Math.random()*500 + 1000;
    startTimer = getTimer();
    myColor = new Color(this);
    myColor.setRGB(rgbValue);
    }


    Actionscript [Show Plain Code]:


    function onLoad(){


    var angle = Math.random()*360;


    vx = Math.cos(angle)*Math.random()*20+5;


    vy = Math.sin(angle)*Math.random()*20+5;


    timeToLast = Math.random()*500 + 1000;


    startTimer = getTimer();


    myColor = new Color(this);


    myColor.setRGB(rgbValue);


    }




    When our particle loads we set up the variables for it. Here we are setting it’s x and y velocities and the time it can last on stage.



    Actionscript [Show Styled Code]:
    myColor = new Color(this);
    myColor.setRGB(rgbValue);


    Actionscript [Show Plain Code]:


    myColor = new Color(this);


    myColor.setRGB(rgbValue);




    This is how we change our Particle’s colour. We create a Color object, myColor and assign it to this particle. We then proceed to give it a rgbValue (which will be passed to the particle later on.)



    Actionscript [Show Styled Code]:
    function onEnterFrame(){
    this._x += vx;
    this._y += vy;
    this._alpha -=2;
    if(getTimer() - startTimer >= timeToLast || this._alpha == 0){
    removeMovieClip(this);
    }
    }


    Actionscript [Show Plain Code]:


    function onEnterFrame(){


    this._x += vx;


    this._y += vy;


    this._alpha -=2;


    if(getTimer() - startTimer >= timeToLast || this._alpha == 0){


    removeMovieClip(this);


    }


    }




    This is where we decide what the particle will do. What this particle is doing at the moment is just moving in a random direction. Its opacity is lowered and if it’s _alpha reaches 0 (or the particle has been on stage for long enough), the particle is removed to free up the cpu.



    This is all we need to manage each particle. Creating the particles is easy. In the main . fla file, enter this code in the timeline (on frame 1.)



    Actionscript [Show Styled Code]:
    var currentParticle = 0;
    var colors = new Array("0xFF0000","0x00FF00","0x0000FF","0xFFFF00","0xFF00FF","0x00FFFF");

    onEnterFrame = function(){
    }

    onMouseDown = function(){
    createExplosion(_xmouse,_ymouse,100,Math.round(Math.random()));
    }

    function createExplosion(x,y,max,sameColor){
    var c;
    if(sameColor){
    c = colors[int(Math.random()*colors.length)];
    }
    for(var i=0; i < max; i++){
    if(!sameColor){
    c = colors[int(Math.random()*colors.length)];
    }
    attachMovie("Particle","P"+currentParticle,getNextHighestDepth(),{_x:x,_y:y,rgbValue:c});
    currentParticle++;
    }

    }


    Actionscript [Show Plain Code]:


    var currentParticle = 0;


    var colors = new Array("0xFF0000","0×00FF00","0×0000FF","0xFFFF00","0xFF00FF","0×00FFFF");





    onEnterFrame = function(){


    }





    onMouseDown = function(){


    createExplosion(_xmouse,_ymouse,100,Math.round(Math.random()));


    }





    function createExplosion(x,y,max,sameColor){


    var c;


    if(sameColor){


    c = colors[int(Math.random()*colors.length)];


    }


    for(var i=0; i < max; i++){


    if(!sameColor){


    c = colors[int(Math.random()*colors.length)];


    }


    attachMovie("Particle","P"+currentParticle,getNextHighestDepth(),{_x:x,_y:y,rgbValue:c});


    currentParticle++;


    }





    }




    The only slightly confusing part of this code is this line:



    Actionscript [Show Styled Code]:
    attachMovie("Particle","P"+currentParticle,getNextHighestDepth(),{_x:x,_y:y,rgbValue:c});


    Actionscript [Show Plain Code]:


    attachMovie("Particle","P"+currentParticle,getNextHighestDepth(),{_x:x,_y:y,rgbValue:c});




    All this does is attaches the MovieClip Particle (which is linked to our Particle.as script) to the movie. It is given the name “P”+currentparticle and is placed at the next highest depth. The last part of this line is where we pass some values to the object (x,y and a colour.)