碰撞后球员的速度永远不会变为零?

时间:2012-05-31 16:29:18

标签: actionscript-3

我的碰撞代码应该反转x速度并将其减小到三分之一,然后当玩家没有按下“A”或“D”键时,速度应该慢慢减少到零,然而,一旦玩家碰撞速度只会减少到.25和.70之间的小小数(根据以前的方向为正或负)。

//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
    }

1 个答案:

答案 0 :(得分:3)

请注意abs(-.25) + abs(.7) ~ 1.0

碰撞将速度设置为非整数(例如2 * .33 ~ .7),因此+/- 1将跳过0而不会降落。

简单的解决方法是将速度保持为整数,例如可以使用Math.floor。 (考虑+/-速度的差异:floor仅向一个方向移动数字。)

快乐的编码。


另外,我不确定int类型在AS3中是如何工作的,这可能值得探讨。