I'm new to Greenfoot, and I am trying to make a simple platformer to start off.
I am having trouble with getting my jumping code working when I press the up arrow key.
Here is part of my code:
I am setting a value for acceleration caused by gravity and a initial velocity, then looping the process of increasing the y-value by the acceleration until the y-coordinate hits the ground again.
But this does not move the object at all when I press the up arrow key.
I feel like I'm not understanding how Greenfoot works, please let me know what I'm doing wrong.
Thanks in advance!
private int grav = -2;
private int x_vel=0;
private int y_vel=0;
public void act()
{
if(Greenfoot.isKeyDown("right")){
move(2);
}
if(Greenfoot.isKeyDown("left")){
move(-2);
}
if(Greenfoot.isKeyDown("up")){
y_vel=6;
int ground=getY();
do{
setLocation(getX(),getY()-y_vel);
y_vel+=grav;
}while(ground!=getY());
//another method I attempted while trying to figure out
//what exactly is causing the problm
/*setLocation(getX(),getY()-6);
setLocation(getX(),getY()-4);
setLocation(getX(),getY()-2);
setLocation(getX(),getY());
setLocation(getX(),getY()+2);
setLocation(getX(),getY()+4);
setLocation(getX(),getY()+6);*/
}
}

