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

2013/4/7

Jumping

Kiara Kiara

2013/4/7

#
I'm trying to make a man class jump, then fall back to the original place. I am sure this is a fairly simple solution, but I just can't grasp it right now. Here is the code: public class Man extends Actor { public void move() { if(Greenfoot.isKeyDown("a")) { left(); } if(Greenfoot.isKeyDown("d")) { right(); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void left() { turn(-1); } public void right() { turn(1); } public void jump() { setLocation(getX(), getY() - 4); } /** * Act - do whatever the Man wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); } }
Gevater_Tod4711 Gevater_Tod4711

2013/4/7

#
Well to make characters jump is not that easy. Therefor you need to declare some new variables and (at least a part of) an engine.
    //you need this variables for a better movement; the first two are the players exact location (double instead of int) and the second two are the movement in x and y direction; 
    private double exactX;
    private double exactY;
    private double velX;
    private double velY;
    
    private static final double GRAVITY = 0.15;
    
    public void setLocation(int x, int y) {
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }
    public void setLocation(double x, double y) {
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }
    
    public double getExactX(){
        return exactX;
    }
    public double getExactY(){
        return exactY;
    }
    
    public void act() {
        if (Greenfoot.isKeyDown("up")) {
            velY = -5;
        }
        if (!onEarth) {//you need to find out when the actor is on the ground and when he is in the air;
            velY += GRAVITY;
        }
        else {
            velY = 0;
        } 
        if(Greenfoot.isKeyDown("a")) {
            left();
        }
        if(Greenfoot.isKeyDown("d")) {
            right();
        }
        move();
    }

    public void move() {
        setLocation(getExactX() + velX, getExactY() + velY);
    }

public void left() { turn(-1); } 
public void right() { turn(1); }
This may is not easy to understand but you realy need some parts of a real engine to do a good movement. You can try to use this engine. This one is realy great. I hope this helps you.
danpost danpost

2013/4/7

#
@Gevater_Todd4711, first I thought that it would seem with the code you gave above that the actor would 'fly' as long as it is off the ground. Then I realized that it may not ever leave the ground. The 'if' statement starting at line 31 says to set the value of 'velY' to zero if the actor is on the ground. No other code deals with the direction of movement along the y-axis after this. Therefore, no jump will occur. If the actor did make it off the ground, the value of 'velY' will decrease by 4.85 every cycle the 'up' key is held down. The actor will quickly move to the top of the window and stay there for a very long time until add enough 0.15 gravity units to offset the decrease before dropping like a feather. I have yet to try the code you gave, but that is how the code appears to me.
Gevater_Tod4711 Gevater_Tod4711

2013/4/7

#
I tryed to make the jump as easy as it's possible but your right this probably will not work. Well it's not easy to make a jump engine that simple.
Gevater_Tod4711 Gevater_Tod4711

2013/4/7

#
I'm currently working on a jump and run engine. It's not ready yet but I think it'll be very easy to use (I hope so). I think I'll be able to upload it in maybe 2 or 3 weeks. Maybe that will make it easyer to programm such thinks.
danpost danpost

2013/4/7

#
OK, so 'dropping like a feather' was not correct. It had all the wait time to increase the velocity, so it dropped pretty quickly. Line 28 should include the condition that the actor is 'onEarth'; and the 'if' block, lines 28 through 30 should be switched with the 'if' block, lines 31 through 36. 'onEarth' should probably be a method that checks the current state of the actor, not a field. Using fields that store information that can be directly obtained by method calls can lead to unwanted behaviour that may be hard to find.
Kiara Kiara

2013/4/7

#
Ummm... I'm not doing this scenario anymore. Thank you both for your help, though.
You need to login to post a reply.