Actionscript 3 玩家速度在碰撞后永远不会变为零?
我的碰撞代码应该反转x速度并将其降低到三分之一,然后当玩家未按“a”或“D”键时,速度应缓慢降低到零,然而,一旦玩家碰撞,速度仅降低到0.25到0.70之间的小小数点(根据之前的方向为正或负)Actionscript 3 玩家速度在碰撞后永远不会变为零?,actionscript-3,Actionscript 3,我的碰撞代码应该反转x速度并将其降低到三分之一,然后当玩家未按“a”或“D”键时,速度应缓慢降低到零,然而,一旦玩家碰撞,速度仅降低到0.25到0.70之间的小小数点(根据之前的方向为正或负) //用于确定如何使用键的函数 函数KeyHandler():void{ //密钥处理程序 if(aKeyPressed==true){ if(数学abs(_vx)
//用于确定如何使用键的函数
函数KeyHandler():void{
//密钥处理程序
if(aKeyPressed==true){
if(数学abs(_vx)0){
_vx+=-1;
}
}
//W密钥处理程序
如果(wKeyPressed==true){
如果(跳转==错误){
_vy=-15;
跳跃=正确;
}
}
else if(wKeyPressed==false){
}
}
//右碰撞代码
如果(右碰撞){
while(右碰撞){
Player.x-=0.1;
RightCollision=false;
如果(_bounders.hitTestPoint((Player.x+(Player.width/2)),(Player.y-(Player.height/2)),true)){RightCollision=true;}
}
_vx*=-.33
}
//左碰撞代码
如果(左碰撞){
while(左碰撞){
Player.x+=0.1;
LeftCollision=false;
如果(_bounders.hitTestPoint((Player.x-(Player.width/2)),(Player.y-(Player.height/2)),true)){LeftCollision=true;}
}
_vx*=-.33
}
注意abs(-.25)+abs(.7)~1.0
碰撞会将速度设置为非整数(例如,2*.33~.7
),因此+/-1将跳过0而不在其上着陆
简单的解决方法是将速度保持为整数,例如,可以使用。(考虑+/-速度的差异:地板
仅向一个方向移动数字。)
快乐编码
此外,我不确定该类型在AS3工作中是如何工作的,这可能是值得探索的。谢谢你,我认为它是这样的,但我无法理解到底是什么。
//Function to determine what to do with keys
function KeyHandler():void{
//A Key Handlers
if(aKeyPressed == true){
if(Math.abs(_vx) < MaxSpeed){
_vx += -6;
}
}
//Player _vx somehow won't hit zero!!!
else if(aKeyPressed == false){
if(_vx < 0){
_vx += 1;
}
}
//D Key Handlers
if(dKeyPressed == true){
if(Math.abs(_vx) < MaxSpeed){
_vx += 6;
}
}
else if(dKeyPressed == false){
if( _vx > 0){
_vx += -1;
}
}
//W Key Handlers
if(wKeyPressed == true){
if(Jumped == false){
_vy = -15;
Jumped = true;
}
}
else if(wKeyPressed == false){
}
}
//Code for Right Collision
if(RightCollision){
while(RightCollision){
Player.x -= 0.1;
RightCollision = false;
if(_boundaries.hitTestPoint((Player.x + (Player.width / 2)), (Player.y - (Player.height / 2)), true)){RightCollision = true;}
}
_vx *= -.33
}
//Code for Left Collision
if(LeftCollision){
while(LeftCollision){
Player.x += 0.1;
LeftCollision = false;
if(_boundaries.hitTestPoint((Player.x - (Player.width / 2)), (Player.y - (Player.height / 2)), true)){LeftCollision = true;}
}
_vx *= -.33
}