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

2015/5/13

attempting to make a recharging meter that stops an action if it is spent

Spooks Spooks

2015/5/13

#
I want to make the boost stop when the recharging meter is spent. however, if I hold onto the shift key, I boost infinitely. When I let go i can't boost again, but i want to make it so that the boost can stop while holding the shift if the meter is spent. Help? boost() just changes the velocity to 5 if that helps
import greenfoot.*;

/**
 * protagonist actions
 * 
 * @author Spooks
 * @version 1.0
 */
public class _hero extends Mover
{
    /**
     * sets velocity and recharge
     */
    public _hero()
    {
        // put your code here
        this.velocity = 2;
        this.rechargemtr = 20;
    }

    /**
     * Act - do whatever the _hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
    } 


    /**
     * Moving actions
     */
    public void move()
    {
        //moves ship accordingly
        if(Greenfoot.isKeyDown("right")){
            moveRight();
        }
        if(Greenfoot.isKeyDown("left")){
            moveLeft();
        }
        if(Greenfoot.isKeyDown("up")){
            moveUp();
        }
        if(Greenfoot.isKeyDown("down")){
            moveDown();
        }
        //speeds up ship if shift is held
        while(Greenfoot.isKeyDown("shift") && rechargemtr != 0){
            boost();
            setImage("heroboost.png");
            rechargemtr--;
            }
        if(!Greenfoot.isKeyDown("shift")){
            velocity = 2;
            setImage("Hero.png");
        }
    }
    /**
     * regenerates boost meter
     */
    public void regeneration(){
        if(rechargemtr < 100 || rechargemtr == 0){
            rechargemtr++;
        }
    }
}

Tommy99 Tommy99

2015/5/13

#
In line 50, switch out the "while" for an "if" and see if that works.
azazeel azazeel

2015/5/13

#
i guess Greenfoot.isKeyDown( ) will be executed many times, are you sure using it instead of Greenfoot.getKey( ) ?
Tommy99 Tommy99

2015/5/13

#
Even better, try this:
if(rechargemtr>0){
         if(Greenfoot.isKeyDown("shift"){
         boost();, etc.


        }

}
Spooks Spooks

2015/5/13

#
Tommy99 wrote...
Even better, try this:
if(rechargemtr>0){
         if(Greenfoot.isKeyDown("shift"){
         boost();, etc.


        }

}
neither worked unfortunately, any other ideas?
Spooks Spooks

2015/5/13

#
Whelp, turns out creating a new void that activates if the rechargemtr is equal to zero works. like so
import greenfoot.*;

/**
 * protagonist actions
 * 
 * @author Spooks
 * @version 1.0
 */
public class _hero extends Mover
{
    private int fireDelay;
    /**
     * sets velocity and recharge
     */
    public _hero()
    {
        // put your code here
        this.velocity = 2;
        this.rechargemtr = 100;
    }

    /**
     * Act - do whatever the _hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        fireDelay ++;
        coolDown();
    } 


    /**
     * Moving actions
     */
    public void move()
    {
        //moves ship accordingly
        if(Greenfoot.isKeyDown("right")){
            moveRight();
        }
        if(Greenfoot.isKeyDown("left")){
            moveLeft();
        }
        if(Greenfoot.isKeyDown("up")){
            moveUp();
        }
        if(Greenfoot.isKeyDown("down")){
            moveDown();
        }
        //speeds up ship if shift is held
        if(rechargemtr>0){
            if(Greenfoot.isKeyDown("shift")){
                boost();
                rechargemtr--;
                setImage("heroboost.png");
                System.out.println(rechargemtr);
            }
        }
        if(Greenfoot.isKeyDown("space") && fireDelay > 8){
            fireDelay = 0;
            _heromissile fire = new _heromissile();
            getWorld().addObject(fire, getX(), getY());
        }
    }
    /**
     * regenerates boost meter
     */
    public void regeneration(){
        if(rechargemtr < 100 || rechargemtr == 0){
            rechargemtr++;
        }
    }
    public void coolDown(){
        if(rechargemtr == 0){
            velocity = 2;
            setImage("Hero.png");
        }
    }
}
danpost danpost

2015/5/13

#
In what method and in what class and under what conditions is the 'regeneration' method of the _hero class called from? If from nowhere, then how is the rechargemtr field recharging?
Spooks Spooks

2015/5/13

#
danpost wrote...
In what method and in what class and under what conditions is the 'regeneration' method of the _hero class called from? If from nowhere, then how is the rechargemtr field recharging?
that's not being called by anything at the moment, just trying to perfect it stopping when the meter is down
danpost danpost

2015/5/13

#
You could add a boolean field and call it 'charging'. Use it to prevent boost, if charging, until recharged (when 'rechargemtr == 100', set 'charging' back to false). Set the 'charging' field to 'true' when 'rechargemtr == 0'. Only recharge ('rechargemtr++') when 'charging' is true). So, you would be doing the following two sets of steps at various places in your code:
// boost test
if (!charging && rechargemtr > 0) // boost, decrease charge, and perform the following test
if (rechagemtr == 0) // set charging to true

// charge test
if (charging) // build charge and perform the following test
if (rechargemtr == 100) // set charging to false
You need to login to post a reply.