flash on 2009-9-30

by pasodania
ByteArray Test
♥0 | Line 66 | Modified 2009-10-01 16:11:28 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.utils.ByteArray;
    import flash.text.TextField;
    
    // ByteArray Test
    public class FlashTest extends Sprite {
        
        private var states:ByteArray;
        private var dispObj:DisplayObject;
        private var txt:TextField;
        
        public function FlashTest() {
            // write as3 code here..
            txt = new TextField();
            states = new ByteArray();
            // 32Byte(256bit)分のデータを作成する
            states.writeUnsignedInt(0);
            states.writeUnsignedInt(0);
            states.writeUnsignedInt(0);
            states.writeUnsignedInt(0);
            states.writeUnsignedInt(0);
            states.writeUnsignedInt(0);
            states.writeUnsignedInt(0);
            states.writeUnsignedInt(0);
            
            dispObj = stage;
            dispObj.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true);
            dispObj.addEventListener(KeyboardEvent.KEY_UP,keyUpListener, false, 0, true );
            dispObj.addEventListener(Event.ACTIVATE,activateListener, false, 0, true );
            dispObj.addEventListener(Event.DEACTIVATE, deactivateListener, false, 0, true );
            txt.autoSize = "left";
            addChild(txt);
        }
        
        private function keyDownListener(ev:KeyboardEvent):void
        {
            txt.appendText("KEYDOWN\n"); 
            // 48~222がkeyCodeの範囲なので2進数で0000 0000の範囲
            // stateの配列インデックスを0 0000にしてどのkeyCodeが来ても0~32(Byte)の範囲にする
            // インデックスにはキーコードの右3桁(0111)を取得し左シフトさせ4bit化(0~16)する
            // 結果的には states[0~31] = 0~15の値が入ることとなる
            states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7);
        }
        
        private function keyUpListener(ev:KeyboardEvent):void
        {
            txt.appendText("KEYUP\n");
            states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7));
        }
        
        private function activateListener( ev:Event ):void
        {
            txt.appendText("ACTIVATE\n");
            // initialize all Bytes
            for( var i:int = 0; i < 32 ; i++ ) states[i] = 0;
            var a:uint = 0x7777;
            var b:uint = ~0x7777;
            var c:int = 14;
            var d:int = ~14;
            txt.appendText("2進数\n");  
            txt.appendText("a : " + a.toString(2) + " | " + 0x7777.toString(2) + "\n");
            txt.appendText("b : " + b.toString(2) + " | " + (~0x7777).toString(2) + "\n");
            txt.appendText("c : " + c.toString(2) + " | " + (14).toString(2) + "\n");
            txt.appendText("d : " + d.toString(2) + " | " + (~14).toString(2) + "\n");

            txt.appendText("16進数\n");            
            txt.appendText("a : " + a.toString(16) + " | " + 0x7777.toString(16) + "\n");
            txt.appendText("b : " + b.toString(16) + " | " + (~0x7777).toString(16) + "\n");
            txt.appendText("c : " + c.toString(16) + " | " + (14).toString(16) + "\n");
            txt.appendText("d : " + d.toString(16) + " | " + (~14).toString(16) + "\n");
        }
        
        private function deactivateListener( ev:Event ):void
        {
            txt.appendText("DEACTIVATE\n");
            for( var i:int = 0; i < 32 ; i++ ) states[i] = 0;
        }
        
        
    }
}