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

2025/1/1

Sprinting problems

DumpsterFire DumpsterFire

2025/1/1

#
In my game, pressing shift makes you sprint. When you're sprinting, it subtracts 3 from how much Sprint you have ('Sprint' is essentially the stamina bar). When not sprinting, it adds 1 until it reaches max Sprint. The problem I'm having is when Sprint reaches 0, it should force the player to stop sprinting, but it doesn't. If shift is still being held when it hits 0, you can continue to sprint, even though you shouldn't be able to. Any help would be greatly appreciated. This is the code I have
public Player()
    {
        speed = 2;
        sprint = 390;
        sprintLock = false;
        sprintCap = true;
        sprintCoolDown = false;
    }

    public void act() 
    {
        Orchard orchard = (Orchard) getWorld();
        int dx = 0, dy = 0;
        if(Greenfoot.isKeyDown("W")) dy = -speed;
        if(Greenfoot.isKeyDown("A")) dx = -speed;
        if(Greenfoot.isKeyDown("S")) dy = speed;
        if(Greenfoot.isKeyDown("D")) dx = speed;        
        setLocation(getX()+dx, getY()+dy);   

        animation();
        animationTimers();
        movement();

        orchard.sprintDisplay.setValue(sprint);

    }    

    public void movement()
    {
        if(sprint == 0)
        {
            sprintLock = true;
            sprintCoolDown = true;
            sprinting = false;
        }
        if(sprint != 390)
        {
            sprintCap = false; 
        }
        else if (sprint == 390)
        {
            sprintCap = true;
            sprintLock = false;
            sprintCoolDown = false;
        }
        if(sprintCoolDown == true)
        {
            sprint = sprint +1;
        }
        if(Greenfoot.isKeyDown("SHIFT")) sprinting = true;
        if(!Greenfoot.isKeyDown("SHIFT")) sprinting = false;

        if(sprinting == false)
        {
            speed = 2;
            if(sprintCoolDown == false)
            {
                if(sprintCap == false)
                {
                    sprint = sprint +1;
                } 
            }
        }  
        if(sprintCoolDown == false)
        {
            if(sprintLock == false)
            {
                if(sprinting == true)
                {
                    speed = 6;
                    sprint = sprint -3;
                }
            }
        }
    }
danpost danpost

2025/1/2

#
DumpsterFire wrote...
In my game, pressing shift makes you sprint. When you're sprinting, it subtracts 3 from how much Sprint you have ('Sprint' is essentially the stamina bar). When not sprinting, it adds 1 until it reaches max Sprint. The problem I'm having is when Sprint reaches 0, it should force the player to stop sprinting, but it doesn't. If shift is still being held when it hits 0, you can continue to sprint, even though you shouldn't be able to. << Code] Omitted >>
Think outside the box. Let the counter continue down below zero until it reaches -300, then negate it. Use its value to determine when to do whatever. Limit the number of 'if's and boolean fields:
public Player() {
    sprint = 300;
}

public void act() {
    speed = 2;
    if (sprint > 0) {
        if (Greenfoot.isKeyDown("SHIFT")) {
            speed = 6;
            sprint -= 3;
        }
    }
    else if (--sprint == -300) sprint = 300;

    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("W")) dy -= speed;
    if (Greenfoot.isKeyDown("A")) dx -= speed;
    if (Greenfoot.isKeyDown("S")) dy += speed;
    if (Greenfoot.isKeyDown("D")) dx += speed;
    setLocation(getX()+dx, getY()+dy);
    
    animation();
    animationTimers();
    
    ((Orchard)getWorld()).sprintDisplay.setValue(Math.abs(sprint));
}
DumpsterFire DumpsterFire

2025/1/2

#
This works exactly how I wanted it to! Thank you very much! I just would like to know what the '--sprint' means. Is that checking if it's in the negatives?
danpost danpost

2025/1/2

#
DumpsterFire wrote...
This works exactly how I wanted it to! Thank you very much! I just would like to know what the '--sprint' means. Is that checking if it's in the negatives?
It is equivalent to each of the following lines, with one exception:
sprint = sprint - 1;
// or
sprint -= 1;
// or
sprint--;
All 3 lines above do exactly the same thing. The difference between the last line and the line you question is when the decrementing takes place. With the minus signs after the field name, the change takes place after any calculations. With the minus signs before the field name, the change (subtracting of 1) is done before any calculations (or comparisons). To be clear, see the page on Assignment, Arithmetic and Unary Operators in the Java Tutorials. The last section on the page deals with unary operators.
You need to login to post a reply.