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

2020/11/11

How to prevent the player from jumping across platforms?

AnJoMorto AnJoMorto

2020/11/11

#
I can't figure out how to prevent the player to jump through the ground from underneath by just jumping. I want the player to be forced to find holes in the ground to be able to access the next floor. I tried to use this but visibly it doesn't work:
 public void act() 
 {        
     //other codes
    moveAround();
 }

public void moveAround()
 {
    //other moving codes
    if(underGround()){ setLocation(getX(), getY() - 10);}
 }

boolean underGround()
    {
        Actor under = getOneObjectAtOffset(getX(), getY() - getImage().getHeight()/2, Ground.class);
        return under != null;
    }
Thanks for the help
danpost danpost

2020/11/11

#
Process your stuff in the following order: a) move horizontally; b) check for obstacles; i) if found, "stop" at obstacle; c) move vertically; d) check for obstacles; i) if found, "stop" at obstacle; To "stop" at obstacle, you will need to know the direction of travel. That will allow you to properly set the location the actor should be at. With 'speed' being the vertical distance traveled, positive value being down:
Actor ground = getOneIntersectingObject(Ground.class);
if (ground != null)
{
    int dir = (int)Math.signum(speed);
    int groundHeight = ground.getImage().getHeight();
    int myHeight = getImage().getHeight();
    setLocation(getX(), ground.getY()-dir*(groundHeight+myHeight+1)/2);
    speed = 0;
}
The sign of the speed value (-1, 0 or 1) placed into dir on line 4 is used to put the actor on the appropriate side of the obstacle. The sign actually should never be zero as no movement should mean no intersecting.
AnJoMorto AnJoMorto

2020/11/11

#
danpost wrote...
Process your stuff in the following order: OMITTED EVERYTHING
I have a problem with where to place the code you gave me:
//1. If I place this here, it would work but I lose the ability to jump while moving.        

        if(onGround() && Greenfoot.isKeyDown(R))
        {
        
            move(playerSpeed);
            setImage(run.getCurrentImage());
            Greenfoot.playSound(runSFX);

        }

        if(onGround() && Greenfoot.isKeyDown(L))
        {
        
            move(-playerSpeed);
            setImage(runL.getCurrentImage());
            Greenfoot.playSound(runSFX);
        
        }        

//2. If I place it here it would work without the ability to jump while moving but also lagging making the Player to jump up and down from a few pixels.
                
        if(Greenfoot.isKeyDown(UP) && onGround() )
        {

            Greenfoot.playSound(jumpSFX);
            vSpeed = jumpHeight;
            fall();
                        
        }

//3. If I place this here it would work, I would still be able to jump while moving, but the lagging problem keeps going, where the Player frenetically jumps up and down.
Sorry for being such a disturbance, for something that will surely end up being a simple thing I missed
danpost danpost

2020/11/11

#
AnJoMorto wrote...
I have a problem with where to place the code you gave me
Put it in your fall method.
AnJoMorto AnJoMorto

2020/11/12

#
danpost wrote...
AnJoMorto wrote...
I have a problem with where to place the code you gave me
Put it in your fall method.
Oh. That works perfectly. Thank you so much, you've helped me a lot! Have an awesome day
You need to login to post a reply.