KEY CONTROL WITH COLLISION DETECTION                as3


    This tutorial shows the basics of key controling of movieclips on stage and collision detection. Useful mainly for games ofcourse also useable for any other application or feature. This example works with key arrows. You can change those keys in conditions simply by changing keyCode. You can find those codes for all the buttons via google very easily. At the end you should get something like this:


     Open actionscript 3.0 flash document. Draw 4 lines which will represent walls, obstacles etc. and convert them to 4 movieclips. Set their instance names to right, left, top, bottom. Place them on stage as you like. Now insert dynamic textfield and name it output. Now create object you want to control with arrows. Convert it to movieclip. Inside this movieclip insert 4 additional keyframes. You can change those as you like. They will represent moving in specific directions. Name this movieclip stickman. Open actions window and insert this actionscript code:


var hittest:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_UP, keyUP);
function keyUP(e:KeyboardEvent){
     stickman.gotoAndStop(1);
	 hittest = false;
}


stage.addEventListener(KeyboardEvent.KEY_DOWN, detectKey);
function detectKey(event:KeyboardEvent):void {
	output.text=("The keypress code is: " + event.keyCode)+"\n"+
					("The x location is: " + stickman.x)+"\n"+
					("The y location is: " + stickman.y)+"\n"+
					("Hit test: " + hittest );
	if (event.keyCode==(Keyboard.LEFT)) {
		stickman.x=stickman.x-5;
		stickman.gotoAndStop(2);
	} else if (event.keyCode==(Keyboard.UP)) {
		stickman.y=stickman.y-5;
		stickman.gotoAndStop(4);
	} else if (event.keyCode==(Keyboard.RIGHT)) {
		stickman.x=stickman.x+5;
		stickman.gotoAndStop(3);
	} else if (event.keyCode==(Keyboard.DOWN)) {
		stickman.y=stickman.y+5;
		stickman.gotoAndStop(5);
	} 
}


stickman.addEventListener(Event.ENTER_FRAME, cellHit);

function cellHit(event:Event):void {
	if (stickman.hitTestObject(right)) {
		stickman.x = stickman.x -5;
		hittest = true;
	}else if (stickman.hitTestObject(left)) {
		stickman.x = stickman.x +5;
		hittest = true;
	}else if (stickman.hitTestObject(top)) {
		stickman.y = stickman.y +5;
		hittest = true;
	}else if (stickman.hitTestObject(bottom)) {	
		stickman.y = stickman.y -5;
		hittest = true;
	}
}


     First line is variable that stores if arrow moveiclip hit one of the walls. Lines 2-6 resets the arrow movieclip and hit variable when no button is pressed. Line 9 is listener which checks if any key is pushed down and then executes detectKey function. This function sends some interesting info to dynamic textfield on stage. This line is optional. If conditions in this function moves the arrow movieclip based on which , in this case, arrow key was pressed and moves this movieclip to corresponding frame and direction. Line 31 is event listener for collision detection. cellHit function checks if stickman movieclip hit any of these 4 wall movieclips. If this is true, it stops the moving of this arrow in this specific direction and set hittest variadble to true.


DOWNLOAD SOURCE FILE