Tag Archives: particles

Simple Particles

Actionscript:
  1. stage.frameRate = 30;
  2. addEventListener(Event.ENTER_FRAME, onLoop);
  3. function onLoop(evt:Event):void{
  4.     if (int(Math.random()*5)==1){
  5.       for (var i:int= 0; i<10; i++) createParticle();
  6.     }
  7. }
  8.  
  9. function createParticle():void{
  10.     var s:MovieClip = new MovieClip();
  11.     s.graphics.beginFill(0);
  12.     s.graphics.drawCircle(0,0,Math.random()*10 + 2);
  13.     s.velX = Math.random()*10-5
  14.     s.velY =  Math.random()*10-5
  15.     s.posX = s.x = 200;
  16.     s.posY = s.y = 200;
  17.     addChild(s);
  18.     s.addEventListener(Event.ENTER_FRAME, onRunParticle);
  19. }
  20.  
  21. function onRunParticle(evt:Event):void {
  22.     var s:MovieClip = MovieClip(evt.currentTarget);
  23.     s.posX += s.velX;
  24.     s.posY += s.velY;
  25.     s.scaleX = s.scaleY -=  .04;
  26.     if (s.scaleX <0){
  27.         removeChild(s);
  28.         s.removeEventListener(Event.ENTER_FRAME, onRunParticle);
  29.     }
  30.     s.x = s.posX;
  31.     s.y = s.posY;
  32. }

Nothing special here. But my students are always asking me about this - in class I have them alter this code to make fire, water and abstract particle systems.

Posted in motion, random | Also tagged , , | 6 Comments