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

2024/4/1

player gets stuck

Jip_maris Jip_maris

2024/4/1

#
hi everyone, I've got a problem I can't fix myself. I'm new to greenfoot and trying to make a platformer game. i already got stuck on the following problem: my character can jump, but it doesn't jump anymore after touching/standing on the big "platform" I made. does anyone have a tip to help me to fix this? I'd really appreciate it :) To give a bit more information, this is the code I use:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Actor;

public class Player1 extends Actor
{
    int vSpeed = 0;
    int accel = 1;
    int jumpHeight = -20;
    public void act()
    {
        moveAround();
        checkFalling();
    }
    private void fall()
    {
        setLocation(getX(),getY()+ vSpeed);
        vSpeed = vSpeed + accel;
    }
    public void moveAround()
    {
        if(Greenfoot.isKeyDown("w"))
        {
            vSpeed = -12;
        }
        if(Greenfoot.isKeyDown("up"))
        {
            vSpeed = -12;
        }
        if(Greenfoot.isKeyDown("a"))
        {
            move (-4);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            move (-4);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            move (4);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            move (4);
        }
    }
    boolean onGround()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, ground.class);
        return under != null;
    }
    private void checkFalling()
    {
        if (onGround() == false)
        {
            fall();
        }
        if (onGround() == true)
        {
            vSpeed = 0;
        }
    }
}
danpost danpost

2024/4/2

#
Jip_maris wrote...
trying to make a platformer game. i already got stuck on the following problem: my character can jump, but it doesn't jump anymore after touching/standing on the big "platform" I made. does anyone have a tip to help me to fix this? I'd really appreciate it :) << Code Omitted >>
I do not condone the onGround method used here. First off, it only checks one point of contact (center of image's base). Secondly, it does not tell you what part of the platform is there at that point (actor could fall or ascent into the platform with any amount of shared space). I prefer to have gravity always in effect (it is naturally, anyway). Have the character move vertically as per its adjusted vertical speed and check for any collision with a platform. The sign of the vertical speed value can be used to determine if bopping head or landing on platform. If a collision with a platform occurs, then reposition the actor (by moving vertically) so that it remains on the same side of the platform and is just off it. You can use the sign of the vertical speed to determine above or under platform. Then zero the vertical speed:
vSpeed++;
setLocation(getX(), getY()+vSpeed);
Actor platform = getOneIntersectingObject(Platform.class);
boolean onGround = false;
if (platform != null) {
    int dy = Math.signum(vSpeed);
    if (dy > 0) onGround = true;
    int platformHeight = platform.getImage().getHeight();
    int height = this.getImage().getHeight();
    setLocation(getX(), platform.getY()-dy*(1+(platformHeight+height)/2));
    vSpeed = 0;
}
if (onGround && (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")) vSpeed = -12;
I added the local boolean variable to hold the on-ground state so it can be used when checking for jumping.
You need to login to post a reply.