I coded jumping and falling sucessfully but as soon as I started doing the code for the platform, nothing worked anymore. The player doesn't fall or jump properly and now my Scenario stopped working at all. I don't know what I did wrong. I'd really appreciate some help!
here's the code I have so far for the player:
public class player extends Actor
{
private final int GRAVITY=1;
private final int SPEED=5;
private int speed1;
public player()
{
speed1=0;
}
public void act()
{
fall();
if(Greenfoot.isKeyDown("w") && isOnSolidGround())
{
jump();
}
move();
}
public void fall()
{
setLocation(getX(),getY()+speed1);
if(isOnSolidGround())
{
speed1=0;
while(isOnSolidGround())
{
setLocation(getX(),getY()-1);
}
setLocation(getX(),getY()+1);
}
else if(speed1<0 && bumpHead())
{
speed1=0;
while(bumpHead())
{
setLocation(getX(),getY()+1);
}
}
else
{
speed1+=GRAVITY;
}
}
public void jump()
{
speed1=-15;
}
public void move()
{
int y=getY();
int x=getX();
if(Greenfoot.isKeyDown("a") && canMoveLeft())
{
x-=SPEED;
}
if(Greenfoot.isKeyDown("d") && canMoveRight())
{
x+=SPEED;
}
setLocation(x,y);
}
public boolean isOnSolidGround()
{
boolean isOnGround=false;
if(getY()>getWorld().getHeight()-2)
{
isOnGround=true;
}
int imageWith=getImage().getWidth();
int imageHeight=getImage().getHeight();
if(getOneObjectAtOffset(imageWith/-2,imageHeight/2,platform.class)!=null ||
getOneObjectAtOffset(imageWith/ 2,imageHeight/2,platform.class)!=null)
{
isOnGround=true;
}
return isOnGround;
}
public boolean bumpHead()
{
boolean bumpedHead=false;
int imageWith=getImage().getWidth();
int imageHeight=getImage().getHeight();
if(getOneObjectAtOffset(imageWith/-2,imageHeight/-2,platform.class)!=null ||
getOneObjectAtOffset(imageWith/ 2,imageHeight/-2,platform.class)!=null)
{
bumpedHead=true;
}
return bumpedHead;
}
public boolean canMoveLeft()
{
boolean canMoveLeft=true;
int imageWith=getImage().getWidth();
int imageHeight=getImage().getHeight();
if(getOneObjectAtOffset(imageWith/-2-SPEED,imageHeight/-2 ,platform.class)!=null ||
getOneObjectAtOffset(imageWith/-2-SPEED,imageHeight/ 2-1,platform.class)!=null)
{
canMoveLeft=false;
}
return canMoveLeft;
}
public boolean canMoveRight()
{
boolean canMoveRight=true;
int imageWith=getImage().getWidth();
int imageHeight=getImage().getHeight();
if(getOneObjectAtOffset(imageWith/ 2+SPEED,imageHeight/-2 ,platform.class)!=null ||
getOneObjectAtOffset(imageWith/ 2+SPEED,imageHeight/ 2-1,platform.class)!=null)
{
canMoveRight=false;
}
return canMoveRight;
}
}