我试图在平台游戏中为我的角色提供一种移动机制,在该机制中按住左键然后再按住右键将导致角色仍然向左移动但速度较慢(即 motionSpeed/2),就像在月球上行走一样(反之亦然):
public var leftKey:Boolean = false;
public var rightKey:Boolean = false;
public var upKey:Boolean = false;
public var leftFlag:Boolean = false;
function ifKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT && rightKey == false)
{
leftKey = true;
if (event.keyCode == Keyboard.LEFT && event.keyCode == Keyboard.RIGHT)
{
leftFlag = true;
trace("leftFlag true");
}
}
if (event.keyCode == Keyboard.RIGHT && leftKey == false)
{
rightKey = true;
}
}
function ifKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
leftKey = false;
leftFlag = false;
}
if (event.keyCode == Keyboard.RIGHT)
{
rightKey = false;
}
}
public function ifEnterFrame(event:Event):void
{
if (leftKey == true && leftFlag == false)
{
player1_mc.x -= mainSpeed;
trace("L");
}
if (rightKey == true && leftFlag == false)
{
player1_mc.x += mainSpeed;
trace("R");
}
if (leftKey == true && rightKey == true)
{
if (leftFlag == true)
{
player1_mc.x -= mainSpeed/2;
trace("L + R");
}
else
{
player1_mc.x += mainSpeed/2;
trace("R + L");
}
}
我的输出看起来像这样: 我按住左键 L L L L 我放开左键。然后,我按住右键 R R R R 我放开右键。然后,我按住右然后也按住左 L R R+L L R R+L 我放开两者。然后,我左握右握 L R R+L L R R+L
虽然我通过我的痕迹知道 leftFlag 没有运行,但我花了几个小时试图找出为什么无济于事。:(