我正在使用AS3制作一个adobe的游戏。我想停止我的广场 它与左边的屏障碰撞而不让它通过。我的盒子的实例名称叫做'box',我的障碍叫做'left'和'right'。
以下是我的舞台图片:image of stage
这是我到目前为止移动盒子的代码:
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
box.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (leftPressed)
{
box.x -= 5;
}
if (rightPressed)
{
box.x += 5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
非常感谢你!
答案 0 :(得分:1)
你需要像
这样的东西if (box.hitTestObject(left)) box.x = left.x + left.width;
if (box.hitTestObject(right)) box.x = right.x - box.width;
添加到fl_MoveInDirectionOfKey
函数