我有个问题。我已经通过摩擦进行了运动并缓解了代码在第一次运行时完美运行,但在第一次运行后无法正常运行。我添加了跟踪命令进行调试,第一次运行后vx值返回 NaN。
leftPressed 是一个布尔值
rightPressed 是一个布尔值
vx 是 x 速度
摩擦是缓出的速度
public var vy:Number = 30;
public var vyInitial:Number;//This is initialised later
public var gravity:Number = 2.0;
public var vx:Number = 0.4;
public var vxInitial:Number;//This is initialised later
public var friction:Number = 0.4;
下面是使用的代码: 包括运行良好的跳跃缓动代码。
if (leftPressed)
{
if (vx == 0)
{
vx = vxInitial;
}
char.x -= vx;
lastMove = "Left";
}
else if (rightPressed)
{
if (vx == 0)
{
vx = vxInitial;
}
char.x += vx;
lastMove = "Right";
}
else if (rightPressed == false && leftPressed == false)
{
if (lastMove == "Right" && rightPressed == false && leftPressed == false)
{
vx -= friction;
trace(vx);
if (vx < 0)
{
lastMove = "No Move";
trace("lastMove Right");
vx = 0;
}
else if (vx > 0)
{
trace("moving left");
char.x += vx;
}
}
else if (lastMove == "Left" && rightPressed == false && leftPressed == false)
{
vx -= friction;
trace(vx);
if (vx < 0)
{
lastMove = "No Move";
trace("lastMove Left");
vx = 0;
}
else if (vx > 0)
{
trace("moving LEft");
char.x -= vx;
}
}
}