2016-03-18 57 views
1

玩家跳躍,但不會回落,如果您按住向上箭頭鍵,玩家會飛/浮動,我該如何解決這個問題,以便玩家退回下?如果提供源代碼會很好,但任何幫助都很好。Greenfoot:跳躍和回落不起作用

import greenfoot.*; 

public class Character extends Actor 
{ 
double Force = 0; 
double Gravity = 0.5; 
double Boost_Speed = -6; 
int Wait = 0; 

public void act() 
{ 
    setLocation(getX(), (int)(getY() + Force)); 
    if(Greenfoot.isKeyDown("up")){ 
     Wait++; 
     Force = Boost_Speed; 
     if(Wait >= 8) 
     { 
      setLocation(getX(), (int)(getY() + 1)); 
      Wait = 0; 
     } 
    } 
    Force = Force + Gravity; 
} 

}

+0

出於好奇...爲什麼你調用'setLocation'兩次嗎? – byxor

回答

0

我通過引入標誌isJumped並獲得與Greenfoot.getKey()方法上次按鍵建議的解決方案:

import greenfoot.*; 

public class Character extends Actor 
{ 
double Force = 0; 
double Gravity = 0.5; 
double Boost_Speed = -6; 
int Wait = 0; 
private String lastKey; 
private Boolean isJumped = false; 
public void act() 
{ 
    setLocation(getX(), (int)(getY() + Force)); 
    lastKey = Greenfoot.getKey(); 
    if(lastKey!=null && lastKey.equals("up") == true && isJumped == true) { 
     isJumped = false; 
    } 
    if(Greenfoot.isKeyDown("up") == true && isJumped == false) { 
     isJumped = true; 
     Wait++; 
     Force = Boost_Speed; 
     if(Wait >= 8) 
     { 
      setLocation(getX(), (int)(getY() + 1)); 
      Wait = 0; 
     } 
    } 
    Force = Force + Gravity; 
} 
}