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

keyboard watcher

ミニゲーム作成用
毎回キーボード入力設定するのメンドクサ!!!
なのでクラスにまとめてみたい。
たとえば黒玉を方向キーで上下左右に動かしてみる。
z押しっぱなしでゆっくり移動。
Get Adobe Flash player
by yukifuruyoru 10 Oct 2010
    Embed
/**
 * Copyright yukifuruyoru ( http://wonderfl.net/user/yukifuruyoru )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vmjH
 */

//ミニゲーム作成用
//毎回キーボード入力設定するのメンドクサ!!!
//なのでクラスにまとめてみたい。
//たとえば黒玉を方向キーで上下左右に動かしてみる。
//z押しっぱなしでゆっくり移動。
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 kw:KeyWatch=new KeyWatch(stage);
        private var key:Vector.<Boolean>=kw.keyVec;
        
        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 KeyWatch extends Object{
    private var stage:Stage;
    public var keyVec:Vector.<Boolean>=new Vector.<Boolean>(128);
    public function KeyWatch(stage:Stage){
        stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
        stage.addEventListener(KeyboardEvent.KEY_UP,onUp);
    }
    private function onDown(e:KeyboardEvent):void{
        keyVec[e.keyCode]=true;
        //trace(e.keyCode);
    }
    private function onUp(e:KeyboardEvent):void{
        keyVec[e.keyCode]=false;
    }
}

/////  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);
            
        }
}