The player jumps but doesn't come back down, and if you hold the up arrow key, the player flies/floats, how can I fix this so the player falls back down? it would be good if source code is provided, but any help is great.
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;
}
}
I suggest the solution by introducing the flag isJumped and getting the last key pressed with Greenfoot.getKey() method:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.