我有一個可以向上,向下,向左和向右移動的MovieClip。如果MovieClip碰到牆上,MC會彈跳,不應該移動過牆壁 - 但如果您繼續按下上,下,左或右鍵,並且不放開 - MovieClip將會越過牆壁。我試圖找出一種辦法,阻止這種情況發生。 link of movement設置MovieClip(AS3)的邊界限制
// 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;
}