I have a MovieClip that can move up, down, left and right. If the MovieClip hits a wall, the MC bounces, and should not move past the wall--but if you continue to press either your up, down, left or right key, and don't let go--the MovieClip will go past the wall. I'm trying to figure out a way, to stop that from happening. link of movement
// function hitWall --------------------------------------------------------------
function hitWall(event:Event):void {
if (box.hitTestObject(wall)) {
box.y+=6;
} else if (box.hitTestObject(wall2)) {
box.y-=6;
} else if (box.hitTestObject(wall3)) {
box.x+=6;
} else if (box.hitTestObject(wall4)) {
box.x-=6;
}
}
// function keyDownEvent ------------------------------------------------------------
function keyDownEvent(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP:
keyUpward = true;
keyDownward = false;
break;
case Keyboard.DOWN:
keyDownward = true;
keyUpward = false;
break;
case Keyboard.LEFT:
keyLeft = true;
keyRight = false;
break;
case Keyboard.RIGHT:
keyRight = true;
keyLeft = false;
break;
}
}
// function keyUpEvent ------------------------------------------------------------
function keyUpEvent(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP:
keyUpward = false;
break;
case Keyboard.DOWN:
keyDownward = false;
break;
case Keyboard.LEFT:
keyLeft = false;
break;
case Keyboard.RIGHT:
keyRight = false;
break;
}
}
// function frameloop ------------------------------------------------------------
function frameloop(event:Event):void {
if (keyUpward) {
forwardMove -=2;
}
if (keyDownward) {
forwardMove += 2;
}
if (keyLeft) {
sideMove -= 2;
}
if (keyRight) {
sideMove += 2;
}
forwardMove +=(0-forwardMove)/inertia;
sideMove +=(0-sideMove)/inertia;
box.y+=forwardMove;
box.x+=sideMove;
}