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

2015/1/31

How to move up and down with just one button.

Jellyfish Jellyfish

2015/1/31

#
Hi I'm making a 2D game where you're looking down on the player and he turns where he's moving, also, I can't figure out how to let him move up and down
danpost danpost

2015/1/31

#
What code do you currently have for the player?
Jellyfish Jellyfish

2015/1/31

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class explorer here.
 * 
import greenfoot.*;

/**
 * 
 */
public class explorer extends players
{
    // The following field will be controlled by a bar that will tell the world to change it
    private int speed = 1;
    
    /**
     * Method act:
     * checks for keypresses and moves the robot the appropriate speed until it comes upon a barrier
     */
    public void act() 
    {
        move();
    }
    
    private void move()
    {
        int dx = 0;
        if (Greenfoot.isKeyDown("a")) dx--;
        if (Greenfoot.isKeyDown("d")) dx++;
        for (int i=0; i<speed; i++)
        {
        setLocation(getX() + dx, getY());
        if (getOneIntersectingObject(floortiles.class) == null) continue;
        setLocation(getX(), getY() + 1);
        if (getOneIntersectingObject(floortiles.class) == null) continue;
        setLocation(getX(), getY() - 2);
        if (getOneIntersectingObject(floortiles.class) == null) continue;
        setLocation(getX() - dx, getY() + 1);
        return;
        }
    }
}
Jellyfish Jellyfish

2015/1/31

#
sorry my description of the game was bad this is an exploring/mining game
danpost danpost

2015/1/31

#
Maybe you should explain exactly what kind of movement you want from the player. Describe how you want your player to move depending on each set of key combinations allowed for movement. Include what you want to happen with each when an obstacle is contacted. Also, please supply the cellsize of your world.
Jellyfish Jellyfish

2015/2/1

#
So, how would I make him jump?
Jellyfish Jellyfish

2015/2/1

#
Also, I want him to move down if there is no block under him (i.e. gravity).
danpost danpost

2015/2/1

#
Jellyfish wrote...
Also, I want him to move down if there is no block under him (i.e. gravity).
Okay. That last word explained it as it was not clear whether the up and down motion was user controlled or what. To add gravity, you need, first off, two or three fields; a constant for the acceleration due to gravity, a variable field for the vertical speed of your actor and possibly a constant for the maximum vertical speed allowed. The gravitational acceleration constant should be added to the vertical speed field every act cycle. If a maximum is applied, now would be the time to check and adjust the value of the vertical speed. Next, the actor is to be moved vertically using the new value of its vertical speed. At this point, before checking for obstacles, you should set up a boolean field to hold the 'on-ground' state of the actor -- set it to false. Intersecting obstacles should now be checked for and, if found, the actor should be moved back off them. The sign of the vertical speed field can be used to determine if the obstacle is above or below the actor. This is used both in determining where to place the actor with respect to the obstacle and the 'on-ground' state can be set to true if the obstacle is below the actor. Upon any obstacle, above or below, being found, the vertical speed should be reset to zero. Finally, the 'on-ground' state can be used in conjunction with a check on a trigger key being down for jumping. You can add yet another constant for the jump strength or just directly set the value of the vertical speed field to some nominal negative number.
Jellyfish Jellyfish

2015/2/2

#
So, what would that be? (Comments next to the code to tell what it does really helps btw).
Jellyfish Jellyfish

2015/2/2

#
danpost wrote...
To add gravity, you need, first off, two or three fields; a constant for the acceleration due to gravity, a variable field for the vertical speed of your actor and possibly a constant for the maximum vertical speed allowed. The gravitational acceleration constant should be added to the vertical speed field every act cycle. If a maximum is applied, now would be the time to check and adjust the value of the vertical speed. Next, the actor is to be moved vertically using the new value of its vertical speed. At this point, before checking for obstacles, you should set up a boolean field to hold the 'on-ground' state of the actor -- set it to false. Intersecting obstacles should now be checked for and, if found, the actor should be moved back off them. The sign of the vertical speed field can be used to determine if the obstacle is above or below the actor. This is used both in determining where to place the actor with respect to the obstacle and the 'on-ground' state can be set to true if the obstacle is below the actor. Upon any obstacle, above or below, being found, the vertical speed should be reset to zero. Finally, the 'on-ground' state can be used in conjunction with a check on a trigger key being down for jumping. You can add yet another constant for the jump strength or just directly set the value of the vertical speed field to some nominal negative number.
I don't know how to do any of that
danpost danpost

2015/2/2

#
Take a look at my Jump and Run Demo w/Moving Platform demo. Run the scenario on the site and click on the buttons along the bottom to view the codes to see how it can be done.
Jellyfish Jellyfish

2015/2/2

#
How can I open that in Greenfoot application?
Jellyfish Jellyfish

2015/2/2

#
nvm
You need to login to post a reply.