flash on 2009-2-28

by awef
♥0 | Line 155 | Modified 2009-02-28 23:37:58 | MIT License
play

ActionScript3 source code

/**
 * Copyright awef ( http://wonderfl.net/user/awef )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/DAid
 */

package
{
    import flash.display.*;
    import flash.events.*;
    
    public class main extends Sprite
    {
        public var o:Control = new Control(stage);
        
        function main()
        {
            for(var i:uint = 0; i < 20; i++)
            {
                o.add_DO(new boundBall(
                    o,
                    Math.round(Math.random() * stage.stageWidth),
                    Math.round(Math.random() * stage.stageHeight),
                    Math.round(Math.random() * 10) + 10,
                    Math.round(Math.random() * 255 * 255 * 255),
                    3
                ));
            }
            for(i = 0; i < 10; i++)
            {
                o.add_DO(new moveBall(
                    o,
                    Math.round(Math.random() * stage.stageWidth),
                    Math.round(Math.random() * stage.stageHeight),
                    Math.round(Math.random() * 10) + 10,
                    3
                ));
            }
            
            stage.addEventListener(Event.ENTER_FRAME, frame);
        }
        
        private function frame(e:Event):void
        {
            o.run();
        }
    }
}

import flash.display.*;
import flash.geom.Point;
import flash.filters.DropShadowFilter;

class Control
{
    private var stage:Stage;
    
    public var obj:Array = new Array();
    
    function Control(arg_stage:Stage)
    {
        stage = arg_stage;
    }
    
    public function add_DO(o:DisplayObject):void
    {
        obj.push(o);
        stage.addChild(o);
    }
    
    public function run():void
    {
        for(var i:String in obj)
        {
            obj[i].run();
        }
    }
}

/*
import flash.display.*;
import flash.geom.Point;
import flash.filters.DropShadowFilter;
*/

class boundBall extends Sprite
{
    public var control:Control;
    public const _class:String = "boundBall";
    
    public var s:uint;
    public var xs:int;
    public var ys:int;
    
    public var r:uint;
    
    function boundBall(arg_control:Control, arg_x:uint, arg_y:uint, arg_r:uint, arg_c:uint, arg_s:uint)
    {
        x = arg_x;
        y = arg_y;
        r = arg_r;
        s = arg_s;
        xs = arg_s;
        ys = arg_s;
        
        control = arg_control;
        
        graphics.beginFill(arg_c);
        graphics.drawCircle(0, 0, r);
        graphics.endFill();
        
        filters = [new DropShadowFilter(5, 45, 0, 0.5)];
    }
    
    public function run():void
    {
        for(var i:String in control.obj)
        {
            if(control.obj[i]._class == "boundBall")
            {
                if(r + control.obj[i].r > Point.distance(new Point(x, y), new Point(control.obj[i].x, control.obj[i].y)))
                {
                    if(x > control.obj[i].x)
                    {
                        xs = s;
                        control.obj[i].xs = -control.obj[i].s;
                    }
                    else if(x < control.obj[i].x)
                    {
                        xs = -s;
                        control.obj[i].xs = control.obj[i].s;
                    }
                    
                    if(y > control.obj[i].y)
                    {
                        ys = s;
                        control.obj[i].ys = -control.obj[i].s;
                    }
                    else if(y < control.obj[i].y)
                    {
                        ys = -s;
                        control.obj[i].ys = control.obj[i].s;
                    }
                }
            }
        }
        
        if(x + r > stage.stageWidth) xs = -s;
        else if(x - r < 0) xs = s;
        if(y + r > stage.stageWidth) ys = -s;
        else if(y - r < 0) ys = s;
        
        x += xs;
        y += ys;
    }
}

/*
import flash.display.*;
import flash.geom.Point;
import flash.filters.DropShadowFilter;
*/
class moveBall extends Sprite
{
    public var control:Control;
    public const _class:String = "moveBall";
    
    public var s:uint;
    public var xs:int;
    public var ys:int;
    
    public var r:uint;
    
    function moveBall(arg_control:Control, arg_x:uint, arg_y:uint, arg_r:uint, arg_s:uint)
    {
        x = arg_x;
        y = arg_y;
        r = arg_r;
        s = arg_s;
        xs = arg_s;
        ys = arg_s;
        
        control = arg_control;
        
        graphics.beginFill(0, 0.2);
        graphics.drawCircle(0, 0, r);
        graphics.endFill();
        
        filters = [new DropShadowFilter(5, 45, 0, 0.5)];
    }
    
    public function run():void
    {
        if(x - r * 3 > stage.stageWidth) xs = -s;
        else if(x + r * 3 < 0) xs = s;
        if(y - r * 3 > stage.stageWidth) ys = -s;
        else if(y + r * 3 < 0) ys = s;
        
        x += xs;
        y += ys;
    }
}