In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Particles

Get Adobe Flash player
by KpaekN 25 Feb 2011
package {
    import flash.events.MouseEvent;
    import flash.display.SimpleButton;
    
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class FlashTest extends Sprite {
        
        private var particles:Array;
        private var particleType:String;
        
        private const NORMAL:String = "normal";
        private const SWIRL:String = "swirl";
        private const WIGGLE:String = "wiggle";
        private const BLAST:String = "blast";
        
        public function FlashTest():void {
            
            particles = new Array();
            stage.addEventListener(Event.ENTER_FRAME, mainLoop);
            particleType = NORMAL;
            
            var normBtn:Sprite = new Sprite();
            normBtn.graphics.beginFill(0x000000);
            normBtn.graphics.drawRect(0, 0, 50, 50);
            normBtn.x = 10;
            normBtn.y = 10;
            stage.addChild(normBtn);
            normBtn.addEventListener(MouseEvent.CLICK, norm);
            
            var swirlBtn:Sprite = new Sprite();
            swirlBtn.graphics.beginFill(0xFF0000);
            swirlBtn.graphics.drawRect(0, 0, 50, 50);
            swirlBtn.x = 70;
            swirlBtn.y = 10;
            stage.addChild(swirlBtn);
            swirlBtn.addEventListener(MouseEvent.CLICK, swirl);
            
            var wiggleBtn:Sprite = new Sprite();
            wiggleBtn.graphics.beginFill(0x00FF00);
            wiggleBtn.graphics.drawRect(0, 0, 50, 50);
            wiggleBtn.x = 130;
            wiggleBtn.y = 10;
            stage.addChild(wiggleBtn);
            wiggleBtn.addEventListener(MouseEvent.CLICK, wiggle);
            
            var blastBtn:Sprite = new Sprite();
            blastBtn.graphics.beginFill(0x0000FF);
            blastBtn.graphics.drawRect(0, 0, 50, 50);
            blastBtn.x = 190;
            blastBtn.y = 10;
            stage.addChild(blastBtn);
            blastBtn.addEventListener(MouseEvent.CLICK, blast);
        }
        
        private function norm(e:MouseEvent):void {
            particleType = NORMAL;
        }
        private function swirl(e:MouseEvent):void {
            particleType = SWIRL;
        }
        private function wiggle(e:MouseEvent):void {
            particleType = WIGGLE;
        }
        private function blast(e:MouseEvent):void {
            particleType = BLAST;
        }

        
        private function mainLoop(e:Event):void {
            
            if(stage.mouseY > 70){
                addParticle(stage.mouseX, stage.mouseY);
            }
            
            for(var i = 0;i < particles.length;i++){
                var p:MovieClip = particles[i];
                if(p.alpha <= 0){
                    stage.removeChild(p);
                    p = null;
                    particles.splice(i, 1);
                }else{
                    if(p.type == WIGGLE){
                        p.angle = Math.random() * 2 * Math.PI;
                    }else if(p.type == SWIRL){
                        p.angle += 0.1;
                    }else if(p.type == BLAST){
                        p.speed += 1;
                    }

                    p.xSpeed = Math.cos(p.angle) * p.speed;
                    p.ySpeed = Math.sin(p.angle) * p.speed;
                    p.x += p.xSpeed;
                    p.y += p.ySpeed;
                    p.speed *= 0.95;
                    p.alpha *= 0.9;
                }
            }

        }
        
        private function addParticle(x:Number, y:Number):void {
            var p:MovieClip = new MovieClip();
            p.graphics.beginFill(Math.random() * 0xFFFFFF);
            p.graphics.drawCircle(0, 0, Math.random() * 2 + 3);
            p.x = x;
            p.y = y;
            p.speed = Math.random() * 3 + 3;
            p.angle = Math.random() * Math.PI * 2;
            p.type = particleType;
            stage.addChild(p);
            particles.push(p);
        }

    }
}