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

2016/11/14

Controls not working and can't find why, also implementing a floor?

Baguette Baguette

2016/11/14

#
I'm working on making a simple game, and part of controlling the player I just can't het to work. Heres the code. I've been messing with it for a while and I can't get it to work the way I want it too. I would also like to implement a floor, where the Player's character stops falling upon coming in contact with an object or a certain Y value.
public class Player extends Actor
{
    private int dy=0;
    private int dg=1;

    public Player()
    {
        setImage("hombre1.png");
    }

    /**
     * Runs on act.
     */
    public void act() 
    {
        addGravity(); 
        doControls();
        changeY();
    } 

    /**
     * Exponentially increases the change in y (dy) of the object. Basically Gravity, but can be set negative to jump.
     */
    public void addGravity()
    {
        dy = dy + dg;
    }

    /**
     * Moves the player left, right, and changes the dy variable to negative to give the effect of a jump.
     */
    public void doControls()
    {
        if (Greenfoot.isKeyDown("up"))
        {
             dy=-5;
        }
        if (Greenfoot.isKeyDown("left"))
        {
            move(-3);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            move(3);
        }
    }
    /**
     * Changes Y of the player by adding variable dy to existing y value.
     */
    public void changeY()
    {
        setLocation(getX(),getY()+dy);
    }
}
Any suggestions and edits are much appreciated. Examples of code similar to this could also be helpful for learning. Probably a simple fix, I'm a code noob :)
danpost danpost

2016/11/14

#
I would say that, so far, everything looks pretty good -- other than the fact that jumps are allowed regardless of "on ground" state.. I think if you separate horizontal movement from vertical movement and have the vertical movement code first move, then check the on ground state before the check for jumping. Also, since adding gravity deals with the vertical speed, it should probably be done at the beginning of the vertical movement code:
// vertical movement
add gravity
move vertically
check for obstacle or y limit
if no obstacle or not at y limit, do nothing (return) else continue with
set dy to zero
position player on obstacle or at lowest position (highest y value)
check for jump key
if key is not pressed, do nothing (return) else continue with
set dy to jump speed
I usually do all this in one method. That way I can set up a local boolean variable to indicate the on ground state. It initial value would be 'false' and be set up before line 4. The checks for obstacles and y limit can be done separately as the positioning of the player would be a bit different for each one. When re-positioning the player, use the positive/negative state of dy to determine where to place the player and only set the onGround field to 'true' if dy is positive. Then, use that field value later to determine if jumping is allowed. You do not have any obstacle checking in place when you move horizontally, as well. Having a variable for the horizontal direction of movement will be needed for re-positioning the player upon contacting an obstacle or edge. Code similar to what I suggest can be found in my Jump and Run Demo w/Moving Platform scenario.
Baguette Baguette

2016/11/14

#
Works brilliantly! Thank you very much. I'm sure I'll be back with more questions. Thanks for a swift response too!
You need to login to post a reply.