forked from: flash on 2010-7-25

by yukifuruyoru forked from flash on 2010-7-25 (diff: 1)
♥0 | Line 135 | Modified 2010-10-11 03:41:13 | MIT License
play

ActionScript3 source code

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

// forked from yukifuruyoru's flash on 2010-7-25
package {
    import flash.geom.Point;
    import flash.display.Sprite;
    import flash.events.Event;
    public class FlashTest extends Sprite {
        private var sv:Point=new Point(0,0);
        private var kbw:KBW=new KBW(stage);
        private var key:Vector.<Boolean>=kbw.kc;
        
        private var com:uint=0;
        private var comlog:uint=0;
        
        public function FlashTest() {
            inittrace(stage);
            trace("--ready--");
            
            graphics.beginFill(0);
            graphics.drawCircle(250,250,16);
            graphics.endFill();
            
            addEventListener(Event.ENTER_FRAME,oef);
        }
        private function oef(...args):void{
            keymap();
            react();
            comlog=com;
        }
        private function keymap():void{
            //←↑→↓z
            com=0;
            if(key[37])com+=0x1;
            if(key[38])com+=0x10;
            if(key[39])com+=0x100;
            if(key[40])com+=0x1000;
            if(key[90])com+=0x10000;
        }
        private function react():void{
            sv.x=0;sv.y=0;
            
            if(com&0x1)sv.x--;
            if(com&0x10)sv.y--;
            if(com&0x100)sv.x++;
            if(com&0x1000)sv.y++;
            
            sv.normalize((com&0x10000)?1:4);
            
            x+=sv.x;
            y+=sv.y;
            
            if(com!=comlog)trace(com.toString(16));//垂れ流し防止
        }

    }
}

//keyboard watcher
import flash.events.KeyboardEvent;

class KBW extends Object{
    private var stage:Stage;
    public var kc:Vector.<Boolean>=new Vector.<Boolean>(128);
    public function KBW(stage:Stage){
        stage.addEventListener(KeyboardEvent.KEY_DOWN,okd);
        stage.addEventListener(KeyboardEvent.KEY_UP,oku);
    }
    private function okd(e:KeyboardEvent):void{
        kc[e.keyCode]=true;
        //trace(e.keyCode);
    }
    private function oku(e:KeyboardEvent):void{
        kc[e.keyCode]=false;
    }
}

class gramas{
    
}


//DNA
import flash.display.BitmapData;
class Dna{
    public var size:Vector.<uint>=Vector.<uint>([1,1]);
    public var palette:Vector.<uint>=Vector.<uint>([0,0xFFffffff]);
    public var map:Vector.<Vector.<uint>>=Vector.<Vector.<uint>>([new Vector.<uint>(1)]);
    public function Dna(code:Array){
        size=Vector.<uint>(code[0]);
        palette=Vector.<uint>(code[1]);
        code[2].forEach(
            function(row:Array,h:uint,...args):void{
                map[h]=Vector.<uint>(row);});
    }
    public function get bmp():BitmapData{
        var sheep:BitmapData=new BitmapData(size[0],size[1],true,palette[0]);
        var L:uint=palette.length;
        map.forEach(function(row:Vector.<uint>,h:uint,...args):void{
            row.forEach(function(c:uint,w:uint,...args):void{if(c&&(c<L)){sheep.setPixel32(w,h,palette[c]);}});
        });
        return sheep;
    }
}
/////  WONDERFL TRACE /////

import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;


function inittrace(s:Stage):void
{
    WTrace.initTrace(s);
}

//global trace function
var trace:Function;

//wtreace class
class WTrace
{
        private static var FONT:String = "Fixedsys";
        private static var SIZE:Number = 12;
        private static var TextFields:Array = [];
        private static var trace_stage:Stage;
        
        public static function initTrace(stg:Stage):void
        {
            trace_stage = stg;
            trace = wtrace;
        }
        
        private static function scrollup():void
        {
            // maximum number of lines: 100
            if (TextFields.length > 100) 
            {
                var removeme:TextField = TextFields.shift();
                trace_stage.removeChild(removeme);
                removeme = null;
            }
            for(var x:Number=0;x<TextFields.length;x++)
            {
                (TextFields[x] as TextField).y -= SIZE*1.2;
            }
        }
    
        public static function wtrace(... args):void
        {
        
            var s:String="";
            var tracefield:TextField;
            
            for (var i:int;i < args.length;i++)
            {
                // imitating flash:
                // putting a space between the parameters
                if (i != 0) s+=" ";
                s+=args[i].toString();
            }
            

            tracefield= new TextField();
            tracefield.autoSize = "left";
            tracefield.text = s;
            tracefield.y = trace_stage.stageHeight - 20;

            var tf:TextFormat = new TextFormat(FONT, SIZE);
            tracefield.setTextFormat(tf);
            trace_stage.addChild(tracefield);
            scrollup();                      
            TextFields.push(tracefield);
            
        }
}