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

2018/6/16

A meter Bar

Venbha Venbha

2018/6/16

#
i've been working on this scenario... where the actor moves toward the mouse. And when you press space, the actor moves faster. I've decided to make the speed boost limited. I want it to have an energy bar and when you press space, the energy should decrease. And when you stand on an energy zone, they should increase the energy meter. Heres my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo etc)

public class Sample extends Actor
{
    public void act() 
    {
        move(5);
        MouseInfo pointer = Greenfoot.getMouseInfo();
        if(pointer != null)
        {
            int mouseX = pointer.getX();
            int mouseY = pointer.getY();
            turnTowards(mouseX, mouseY);
            int button = pointer.getButton();
        }
        if(Greenfoot.isKeyDown("space"))
        {
            move(10);
        }
    }    
}
Should I edit it and create a new class for the meter? or should JUST create the new class for meter?
Venbha Venbha

2018/6/16

#
Also: I want its meter below it!
danpost danpost

2018/6/16

#
Venbha wrote...
Should I edit it and create a new class for the meter? or should JUST create the new class for meter?
Of course you will have to edit it to work with the value of the meter. The meter should be held in a field within the Sample class:
private Actor turboMeter;
A meter is created, added to the world and assigned to turboMeter. The act method of the Sample class can, as the last thing it does, call a method in the class of the meter so it can update its position or remove itself from the world:
// last line in Sample act method
turboMeter.updatePosition(this);

// method in class of meter
public void updatePosition(Actor actor)
{
    if (actor.getWorld() == null) getWorld().removeObject(this);
    else setLocation(actor.getX(), actor.getY()+actor.getImage().getHeight()/2+5);
}
You need to login to post a reply.