package { import flash.ui.Keyboard; public class PlayerInputController { public var player:PlayerModel; private var keysDown:Vector.; public function PlayerInputController(player:PlayerModel) { this.player = player; keysDown = new Vector.(4, true); } public function keyDown(keyCode:int):void { switch (keyCode) { case Keyboard.LEFT: player.startMoving(Direction.WEST); keysDown[Direction.WEST] = true; break; case Keyboard.RIGHT: player.startMoving(Direction.EAST); keysDown[Direction.EAST] = true; break; case Keyboard.UP: player.startMoving(Direction.NORTH); keysDown[Direction.NORTH] = true; break; case Keyboard.DOWN: player.startMoving(Direction.SOUTH); keysDown[Direction.SOUTH] = true; break; case Keyboard.SPACE: player.interact(); break; } } public function keyUp(keyCode:int):void { switch (keyCode) { case Keyboard.LEFT: player.stopMoving(Direction.WEST); keysDown[Direction.WEST] = false; if (player.facing == Direction.WEST) { if (keysDown[Direction.NORTH]) { player.startMoving(Direction.NORTH); } else if (keysDown[Direction.SOUTH]) { player.startMoving(Direction.SOUTH); } else if (keysDown[Direction.EAST]) { player.startMoving(Direction.EAST); } } break; case Keyboard.RIGHT: player.stopMoving(Direction.EAST); keysDown[Direction.EAST] = false; if (player.facing == Direction.EAST) { if (keysDown[Direction.EAST]) { player.startMoving(Direction.NORTH); } else if (keysDown[Direction.SOUTH]) { player.startMoving(Direction.SOUTH); } else if (keysDown[Direction.WEST]) { player.startMoving(Direction.WEST); } } break; case Keyboard.UP: player.stopMoving(Direction.NORTH); keysDown[Direction.NORTH] = false; if (player.facing == Direction.NORTH) { if (keysDown[Direction.WEST]) { player.startMoving(Direction.WEST); } else if (keysDown[Direction.EAST]) { player.startMoving(Direction.EAST); } else if (keysDown[Direction.SOUTH]) { player.startMoving(Direction.SOUTH); } } break; case Keyboard.DOWN: player.stopMoving(Direction.SOUTH); keysDown[Direction.SOUTH] = false; if (player.facing == Direction.SOUTH) { if (keysDown[Direction.WEST]) { player.startMoving(Direction.WEST); } else if (keysDown[Direction.EAST]) { player.startMoving(Direction.EAST); } else if (keysDown[Direction.NORTH]) { player.startMoving(Direction.NORTH); } } break; } } } }