Move by KeyBoard in hittable

by pasodania
♥0 | Line 69 | Modified 2009-07-23 10:09:11 | 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/eyid
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class FlashTest extends Sprite {
        
        
        private const _W:int = 10;
        private const _H:int = 10;
        private var hittable:Array = [
            [ 1,1,1,1,1,1,1 ],
            [ 1,1,0,0,1,1,1 ],
            [ 1,0,1,0,0,0,1 ],
            [ 1,0,0,0,1,1,1 ],
            [ 1,0,0,0,0,0,1 ],
            [ 1,1,1,1,1,1,1 ]
        ];
        
        public var my_x:int;
        public var my_y:int;
        
        public function FlashTest() {
            // write as3 code here..
            
            my_x = 3;
            my_y = 3;
            CreateMap();
            
            this.graphics.beginFill(0xFFAAFF);
            this.graphics.drawCircle(_W*my_x+_W/2, _H*my_y+_H/2, 5);
            this.graphics.endFill();
            
            this.stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown);
        }
        
        private function onKeyDown(e:KeyboardEvent):void{
            // Move Check
            var pre_x:int = my_x;
            var pre_y:int = my_y;
            
            switch(e.keyCode){
            case Keyboard.UP:
                if(hittable[pre_y-1][pre_x] == 0) pre_y--;
                break;
            case Keyboard.DOWN:
                if(hittable[pre_y+1][pre_x] == 0) pre_y++;
                break;
            case Keyboard.LEFT:
                if(hittable[pre_y][pre_x-1] == 0) pre_x--;
                break;
            case Keyboard.RIGHT:
                if(hittable[pre_y][pre_x+1] == 0) pre_x++;
                break;
            defaut:break;
            }
            
            // Init
            if( pre_x != my_x || pre_y != my_y ){
                this.graphics.beginFill(0xFFFFFF);
                this.graphics.drawCircle(_W*my_x+_W/2, _H*my_y+_H/2, 5);
                this.graphics.endFill();
                my_x = pre_x;
                my_y = pre_y;
                this.graphics.beginFill(0xFFAAFF);
                this.graphics.drawCircle(_W*my_x+_W/2, _H*my_y+_H/2, 5);
                this.graphics.endFill();
            }
        }
        
       
        private function CreateMap():void{
            for(var i:int = 0; i < hittable.length; i++){
                for(var k:int = 0; k < hittable[i].length; k++){
                    if(hittable[i][k] == 1){
                        this.graphics.beginFill(0x000000);
                        this.graphics.drawRect(_W*k, _H*i, 10, 10);
                        this.graphics.endFill();
                    }
                }
            }   
        }
    }
}