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

Bitmapのテスト

Get Adobe Flash player
by awef 10 Mar 2009
    Embed
/**
 * Copyright awef ( http://wonderfl.net/user/awef )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3yzI
 */

package
{
    import flash.display.*;
    import flash.events.*;
    
    public class main extends Sprite
    {
        public var Cluster:Object = {};
        
        private var bmp_data:BitmapData;
        private var bmp:Bitmap;
        
        function main()
        {
            bmp_data = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);
            bmp = new Bitmap(bmp_data);
            stage.addChild(bmp);
            stage.addEventListener(Event.ENTER_FRAME, frame);
            
            for(var i:uint = 0; i < 45; i++)
            {
                stage.addChild(new ball(
                    Cluster,
                    stage,
                    Math.floor(Math.random() * stage.stageWidth),
                    Math.floor(Math.random() * stage.stageHeight),
                    10,
                    Math.floor(Math.random() * 0xFFFFFF),
                    4
                ));
            }
        }
        
        private function frame(e:Event):void
        {
            bmp_data.draw(stage);
            bmp.bitmapData = bmp_data;
        }
    }
}

import flash.display.*;
import flash.events.*;
//import flash.geom.Point;

class Cluster_item extends Sprite
{
    protected var Cluster:Object;
    
    public function addToCluster(name:String):void
    {
        //todo:重複防止
        if(Cluster[name] == null)
            Cluster[name] = [this];
        else
            Cluster[name].push(this);
    }
    
    /*未実装
    public function removeFromCluster(name:String):void
    {
    }
    */
    
    public function remove():void
    {
        beforeRemove();
        stage.removeChild(this);
    }
    
    public function beforeRemove():void { }
}


class ball extends Cluster_item
{
    public var c:uint;
    public var r:uint;
    
    public var s:uint;
    public var xs:int;
    public var ys:int;
    
    function ball(arg_Cluster:Object, arg_stage:Stage, arg_x:int, arg_y:int, arg_r:uint, arg_c:uint, arg_s:uint)
    {
        Cluster = arg_Cluster;
        
        x = arg_x;
        y = arg_y;
        c = arg_c;
        r = arg_r;
        
        draw();
        
        s = arg_s;
        xs = s;
        ys = s;
        
        addToCluster("ball");
        
        arg_stage.addEventListener(Event.ENTER_FRAME, frame);
    }
    
    public override function beforeRemove():void
    {
        stage.removeEventListener(Event.ENTER_FRAME, frame);
    }
    
    public function draw():void
    {
        graphics.clear();
        graphics.beginFill(c);
        graphics.drawCircle(0, 0, r);
        graphics.endFill();
    }
    
    public function frame(e:Event):void
    {
        var o:Object;
        for(var tmp:String in Cluster["ball"])
        {
            o = Cluster["ball"][tmp];
            //if(r + o.r > Point.distance(new Point(x, y), new Point(o.x, o.y)))
            if(r + o.r > Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2)))
            {
                if(x > o.x)
                {
                    xs = s;
                    o.xs = -o.s;
                }
                else if(x < o.x)
                {
                    xs = -s;
                    o.xs = o.s;
                }
                
                if(y > o.y)
                {
                    ys = s;
                    o.ys = -o.s;
                }
                else if(y < o.y)
                {
                    ys = -s;
                    o.ys = o.s;
                }
            }
        }
        
        if(x + r > stage.stageWidth) xs = -s;
        else if(x - r < 0) xs = s;
        if(y + r > stage.stageHeight) ys = -s;
        else if(y - r < 0) ys = s;
        
        x += xs;
        y += ys;
    }
}