How to Make a Maze in Flash ActionScript 3.0

Step 1: Make a new layer called "maze". Make a maze on that layer. Convert            it to a symbol. Name it whatever you want. Make its instance name            "maze".

Step 2: Do step 1 for the player (circle) except make its instance name             "player" and make the layer "player".

Step 3: Insert the code below this sticky note on a new layer named               "actions".

Note: There might be a few errors when I typed this code. Please leave a           comment in the comment box at the bottom of the page if there are           any errors. I used Flash Professional CS5 to make this maze, but CS4           might work better for this maze. I haven't tested CS3 or any other           version yet.

 

	var rightArrow:Boolean = false;	
	var leftArrow:Boolean = false;
	var upArrow:Boolean = false;
	var downArrow:Boolean = false;
	var speed:int = 5;

	stage.addEventListener(KeyboardEvent.KEY_DOWN, stage_onKeyDown);
	stage.addEventListener(KeyboardEvent.KEY_UP, stage_onKeyUp);
	stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);

	function stage_onKeyDown(event:KeyboardEvent):void {
		if(event.keyCode == Keyboard.RIGHT) rightArrow = true;
		if(event.keyCode == Keyboard.LEFT) leftArrow = true;
		if(event.keyCode == Keyboard.UP) upArrow = true;
		if(event.keyCode == Keyboard.DOWN) downArrow = true;
	}
	function stage_onKeyUp(event:KeyboardEvent):void {
		if(event.keyCode == Keyboard.RIGHT) rightArrow = false;
		if(event.keyCode == Keyboard.LEFT) leftArrow = false;
		if(event.keyCode == Keyboard.UP) upArrow = false;
		if(event.keyCode == Keyboard.DOWN) downArrow = false;
	}
	function stage_onEnterFrame(event:Event):void {
		var rect:Rectangle = player.getBounds(this);
		var i:int = 0;
		var xBump:int = 0;
		var yBump:int = 0;
		if(rightArrow) {
			xBump = speed;
			for(i = 0; i < speed; i++) {
				if(maze.hitTestPoint(rect.right + i, player.y, true)) {
					xBump = i - 1;
					break;
				}
			}
		}
		if(leftArrow) {
			xBump = -speed;
			for(i = 0; i < speed; i++) {
				if(maze.hitTestPoint(rect.left - i, player.y, true)) {
					xBump = -i + 1;
					break;
				}
			}
		}
		if(upArrow) {
			yBump = -speed;
			for(i = 0; i < speed; i++) {
				if(maze.hitTestPoint(player.x, rect.top - i, true)) {
					yBump = -i + 1;
					break;
				}
			}
		}
		if(downArrow) {
			yBump = speed;
			for(i = 0; i < speed; i++) {
				if(maze.hitTestPoint(player.x, rect.bottom + i, true)) {
					yBump = i - 1;
					break;
				}
			}
		}
		player.x += xBump;
		player.y += yBump;
	}
HTML Comment Box is loading comments...