So, I'm programming a jump& run type of game and my character can jump on platforms and fall off them. I did some animation with left & right movement and also jumping. However, I have no Idea how to set the image to a falling.png if the character falls off a platform. Also it would be neat if he changes the image after comming back down from a jump. I'd really appreciate some help!
heres the code I have so far :
public class player extends Actor
{
private final int GRAVITY=1;
private final int SPEED=5;
private int speed1;
private GreenfootImage stand1 = new GreenfootImage("stand1.png");
private GreenfootImage stand2 = new GreenfootImage("stand2.png");
private GreenfootImage run1 = new GreenfootImage("run1.png");
private GreenfootImage run2 = new GreenfootImage("run2.png");
private GreenfootImage run3 = new GreenfootImage("run3.png");
private GreenfootImage run4 = new GreenfootImage("run4.png");
private GreenfootImage jump1 = new GreenfootImage("jump1.png");
private GreenfootImage jump2 = new GreenfootImage("jump2.png");
private GreenfootImage fall1 = new GreenfootImage("fall1.png");
private GreenfootImage fall2 = new GreenfootImage("fall2.png");
private int frame=1;
private int counter=0;
private int blick;
public player()
{
speed1=0;
}
public void act()
{
fall();
jump();
move();
stand();
counter++;
}
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);
if(blick==1)
{
setImage(fall1);
}
if(blick==0)
{
setImage(fall2);
}
}
}
else
{
speed1+=GRAVITY;
}
}
public void jump()
{
if(Greenfoot.isKeyDown("w") && isOnSolidGround())
{
speed1=-15;
if(blick==1)
{
setImage(jump1);
}
if(blick==0)
{
setImage(jump2);
}
}
}
public void move()
{
int y=getY();
int x=getX();
if(Greenfoot.isKeyDown("a") && canMoveLeft())
{
x-=SPEED;
if(counter % 10 == 0 && isOnSolidGround())
{
animateLeft();
}
blick=0;
}
if(Greenfoot.isKeyDown("d") && canMoveRight())
{
x+=SPEED;
if(counter % 10 == 0 && isOnSolidGround())
{
animateRight();
}
blick=1;
}
setLocation(x,y);
}
public void animateRight()
{
if(frame == 1)
{
setImage(run1);
}
else if(frame == 2)
{
setImage(run2);
frame = 1;
return;
}
frame++;
}
public void animateLeft()
{
if(frame == 1)
{
setImage(run3);
}
else if(frame == 2)
{
setImage(run4);
frame = 1;
return;
}
frame++;
}
public void stand()
{
if(!Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d") && !Greenfoot.isKeyDown("w") && isOnSolidGround())
{
if(blick==1)
{
setImage(stand1);
}
if(blick==0)
{
setImage(stand2);
}
}
}
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;
}
}
