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

2020/11/12

Trouble with double jump mechanic

thatkidwhoneedshelp thatkidwhoneedshelp

2020/11/12

#
Hello! I'm trying to build a stick figure platformer game, but whenever I try to program a double jump function, my player just loses his ability to jump entirely. Here's the code for the actor class: I believe the problem is in the if onPlatform == true section
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Runner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Runner extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private int jumpHeight = -13;
    private boolean hasStarted = false;
    private int jumpCount = 0;
    
    private GreenfootImage run1 = new GreenfootImage("rsz_frame_1.png");
    //run1.scale(run1.getWidth() / 2, run1.getHeight() / 2);
    private GreenfootImage run2 = new GreenfootImage("rsz_frame_2.png");
    private GreenfootImage run3 = new GreenfootImage("rsz_frame_3.png");
    private GreenfootImage run4 = new GreenfootImage("rsz_frame_4.png");
    private int animationCounter = 0;
    
    private int frame = 1;
    //ADDED CODE
    private boolean jumped = false;
    
    public Runner() {
        //getImage().scale(getImage().getWidth() / 2,getImage().getHeight() / 2);
    }
    
    /**
     * Act - do whatever the Runner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        while (hasStarted == false) {
            if (Greenfoot.isKeyDown("w")) {
                hasStarted = true;
            }
        }
        moveAround();
        checkFalling();
        if (onPlatform() == true) {
            move(-3);
            if (Greenfoot.isKeyDown("space")) {
                vSpeed = jumpHeight;
                jumped = true;
                fall();
            }
        }
        if (onPlatform() == false) {
            move(-3);
            if (Greenfoot.isKeyDown("space") && jumped == true) {
                vSpeed = jumpHeight + 7;
                fall();
                jumped = false;
            }
        
        }
        if (getY() > 510) {
            Greenfoot.stop();
        }
        
        animationCounter++;
    }
    
    public void moveAround() {
        if (Greenfoot.isKeyDown("d")){
            move(7);
            if (animationCounter % 4 == 0)
            {
                animateRight();
            }
        }
        if (Greenfoot.isKeyDown("a")){
            move(-7);
        }
    }
    
    private void fall() {
        setLocation(getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
    public boolean onPlatform() {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        if (under != null) {
            return true;
        } else {
            return false;
        }
    }
    
    public void checkFalling() {
        if (onPlatform() == false) {
            fall();
        }
    }
    
    public void animateRight()
    {
        if (frame == 1)
        {
            setImage(run1);
        }
        else if (frame == 2)
        {
           setImage(run2);
        }
        else if (frame == 3)
        {
            setImage(run3);
        }
        else if (frame == 4)
        {
            setImage(run4);
            frame = 1;
            return;
        }
        frame++;
        
    }
}
danpost danpost

2020/11/12

#
thatkidwhoneedshelp wrote...
whenever I try to program a double jump function, my player just loses his ability to jump entirely
You need to subtract on line 56.
thatkidwhoneedshelp thatkidwhoneedshelp

2020/11/14

#
danpost wrote...
thatkidwhoneedshelp wrote...
whenever I try to program a double jump function, my player just loses his ability to jump entirely
You need to subtract on line 56.
I tried this and it only makes the character jump higher. How do I add a pause in between jumps?
danpost danpost

2020/11/15

#
thatkidwhoneedshelp wrote...
I tried this and it only makes the character jump higher. How do I add a pause in between jumps?
What -- you do not want higher? Okay, explain then what you mean by double jump -- in detail.
thatkidwhoneedshelp thatkidwhoneedshelp

2020/11/17

#
danpost wrote...
thatkidwhoneedshelp wrote...
I tried this and it only makes the character jump higher. How do I add a pause in between jumps?
What -- you do not want higher? Okay, explain then what you mean by double jump -- in detail.
I want the character to jump and then be able to wait x amount of seconds until they can jump a second time in mid air. Currently, the character is able to jump fairly high, but only once. If I press the spacebar again while the character is in mid air it will not do anything. How can I fix this?
danpost danpost

2020/11/17

#
You will need to track the state of the "space" key:
private boolean spaceDown;
tracking its state with the following:
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = !spaceDown;
    // (a)
}
To do (or potentially do) something when the key is pressed down (going from up to down), you would want the following at line (a):
if (spaceDown)
{
    // (b)
}
Now, you can test the on ground and single jump states and apply the jumping code at (b). The falling code will be separate from any of this.
thatkidwhoneedshelp thatkidwhoneedshelp

2020/11/18

#
danpost wrote...
You will need to track the state of the "space" key:
private boolean spaceDown;
tracking its state with the following:
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = !spaceDown;
    // (a)
}
To do (or potentially do) something when the key is pressed down (going from up to down), you would want the following at line (a):
if (spaceDown)
{
    // (b)
}
Now, you can test the on ground and single jump states and apply the jumping code at (b). The falling code will be separate from any of this.
Could you explain a little further? I'm sorry but I'm really new to this whole process
You need to login to post a reply.