im trying to make a platformer game, but i cant seem to get my character to stay on the "platform", he falls through it regardless, any help please?
below is all of my relevant code
public class MainCharacter extends Actor
{
private String imageFile = "CharacterFacingRightTakingStep.png";
GreenfootImage baseImage = new GreenfootImage(imageFile);
private int ySpeed;
private int apexTimer;
private int vSpeed = 0;
private int acceleration = 1;
static final int CHG_RATE = 10; // adjust to suit
int chgImgIn = 1;
int imgNum = 1;
String[] images = { "CharacterFacingRight.png", "CharacterFacingRightTakingStep.png" };
public MainCharacter()
{
}
public void checkFall()
{
if (onGround()){
vSpeed = 0;
}
else { fall();
}
}
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
public boolean onGround()
{
Object under = getOneIntersectingObject(platform.class);
return (under != null);
}
public boolean groundLevel()
{
Object under = getOneIntersectingObject(platform.class);
return under != null;
}
public void act()
{
int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
boolean onGround = (getY() == groundLevel);
if (!onGround) // in middle of jump
{
if (ySpeed == 0 && apexTimer > 0) apexTimer--; // run apex timer
if (ySpeed == 0 && apexTimer > 0) return;
ySpeed++;
setLocation(getX(), getY()+ySpeed);
if (getY()>=groundLevel)
{
setLocation(getX(), groundLevel);
}
}
else // on ground
{
if ("space".equals(Greenfoot.getKey()))
{
ySpeed = -5;
setLocation(getX(), getY()+ySpeed);
apexTimer = 5;
}
}
if (chgImgIn == 0)
{
chgImgIn = CHG_RATE; // reset countdown
imgNum = (imgNum + 1) % 2;
setImage(images[imgNum]);
}
{
if(Greenfoot.isKeyDown("left"))
{
setLocation(getX()-1, getY());
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+1, getY());
}
}
}
}
