This site requires JavaScript, please enable it in your browser!
Greenfoot back
AdamSteele
AdamSteele wrote ...

2016/4/21

My character keeps falling through the platform

AdamSteele AdamSteele

2016/4/21

#
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());
        }
    }
}
}
danpost danpost

2016/4/21

#
The main problem that beginners have when creating a "jump-n-run" type scenario is that they do not account for the vertical direction of movement when performing collision checking. The direction of movement will determine whether the jumper should be positioned above or below the object collided with. In either case, the vertical speed should be reset to zero. In your code, I do not see any repositioning of the jumper when a collision occurs. Also, your 'onGround' method does not really determine if the platform is really 'under' the jumper. By using the sign of the vertical speed (the direction of movement vertically), this can be determined.
You need to login to post a reply.