【问题标题】:Greenfoot: Jumping and coming back down doesn't work绿脚:跳跃和回来是行不通的
【发布时间】:2016-03-18 21:25:19
【问题描述】:

玩家跳了但没有下来,如果你按住向上箭头键,玩家会飞/漂浮,我该如何解决这个问题让玩家掉下来?如果提供源代码就好了,但任何帮助都很好。

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

}

【问题讨论】:

  • 出于好奇...你为什么要给setLocation打两次电话?

标签: java greenfoot


【解决方案1】:

我通过引入标志 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;
} 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多