半マス移動

by atsushi015
♥0 | Line 82 | Modified 2012-10-31 16:18:48 | MIT License
play

ActionScript3 source code

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

// forked from atsushi015's flash on 2010-4-28
package {
    import flash.display.Sprite;
    import flash.accessibility.Accessibility;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    
    public class FlashTest extends Sprite {
            private const quarterMap:Array = new Array(
                new Array(1, 1, 1, 1, 1, 1, 1, 1),
                new Array(1, 0, 0, 0, 0, 0, 0, 1),
                new Array(1, 0, 0, 1, 0, 0, 0, 1),
                new Array(1, 0, 0, 0, 0, 0, 0, 1),
                new Array(1, 0, 0, 0, 0, 0, 0, 1),
                new Array(1, 0, 0, 0, 0, 1, 0, 1),
                new Array(1, 0, 0, 0, 0, 0, 0, 1),
                new Array(1, 1, 1, 1, 1, 1, 1, 1)
                );
            private const tileSize:int = 32;
            private const gridSize:int = tileSize / 2;
            
            private var charX:int = 2;
            private var charY:int = 2;
            private var charSprite:Shape;
                
        public function FlashTest() {
            // write as3 code here..
            graphics.lineStyle(1);
            
            for (var i:int = 0; i < quarterMap.length; i++) {
                    for (var j:int = 0; j < quarterMap[i].length; j++) {
                        switch (quarterMap[i][j]) {
                            case 1:
                        graphics.drawRect(j * gridSize*2, i * gridSize*2, tileSize, tileSize);
                        break;
                        }
                    }
            }
            
            charSprite = new Shape;
            charSprite.graphics.lineStyle(1);
            charSprite.graphics.drawCircle(tileSize/2, tileSize/2, tileSize/2);
            addChild(charSprite);
                updateChar();
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown_);
        }
        
        private function KeyDown_(event:KeyboardEvent):void {
                switch (event.keyCode) {
                    case Keyboard.UP:
                        // 素直に総当たり判定すればいいのかも知れないが・・・
                        if ( (charY%2 == 1) ||
                             ( (quarterMap[charY/2-1][int(charX/2)] == 0) &&
                               (charX%2 == 0 || quarterMap[charY/2-1][int(charX/2)+1] == 0)
                             ) )    
                            charY--;
                        break;
                        
                    case Keyboard.DOWN:
                        if ( (charY%2 == 1) ||
                             ( (quarterMap[charY/2+1][int(charX/2)] == 0) &&
                               (charX%2 == 0 || quarterMap[charY/2+1][int(charX/2)+1] == 0)
                             ) )    
                            charY++;
                        break;
                        
                    case Keyboard.LEFT:
                        if ( (charX%2 == 1) ||
                             ( (quarterMap[int(charY/2)][charX/2-1] == 0) &&
                               (charY%2 == 0 || quarterMap[int(charY/2)+1][charX/2-1] == 0)
                             ) )    
                            charX--;
                        break;
                        
                    case Keyboard.RIGHT:
                        if ( (charX%2 == 1) ||
                             ( (quarterMap[int(charY/2)][charX/2+1] == 0) &&
                               (charY%2 == 0 || quarterMap[int(charY/2)+1][charX/2+1] == 0)
                             ) )    
                            charX++;
                        break;
                        
                        default:
                        return;
                }
                
                updateChar();
        }
        
        private function updateChar():void {
                        charSprite.x = charX * gridSize;
                        charSprite.y = charY * gridSize;
        }
    }
}