2012-05-31 43 views
3

我的碰撞代碼應該反轉x的速度並將其降低到三分之一,然後當玩家沒有按下「A」或「D」鍵時,速度應該慢慢降低到零,但是,一旦玩家碰撞速度只降低到0.25和0.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 
    } 

回答

3

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

相撞速度設置的東西,不是整數(例如2 * .33 ~ .7),所以+/- 1將跳過0而不落地。

簡單的解決方法是保持速度爲一個整數,例如可以用Math.floor來完成。 (考慮+/-速度的差異:floor僅向一個方向移動編號。)

快樂編碼。


而且,我不知道該int類型在AS3工作,這可能是值得探討的是如何工作的。

+0

謝謝你,我覺得它是這樣的,但只是無法包裹我的頭。 – Cory