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

2022/5/26

life or gasoline

elvacan elvacan

2022/5/26

#
How do I make a life bar that goes down slowly, that is, I have 100 life and it goes down 10 times and uses images as life, that is, I have a line that is made by images and when a time passes, like, 30 second travel a percentage of your life
danpost danpost

2022/5/26

#
elvacan wrote...
How do I make a life bar that goes down slowly, that is, I have 100 life and it goes down 10 times and uses images as life, that is, I have a line that is made by images and when a time passes, like, 30 second travel a percentage of your life
Use an int time counter field. Increment it every act step. Use the modulus operation to make it loop back to zero at a value of 1800 to approximate 30 seconds of time. Test its value after incrementing. When its value is zero, subtract from life. The bar can be treated as being from 0 to 10 since you want it to jump by 10s. If you display the value as text as well, just multiply by 10. Update bar image only when its value changes.
elvacan elvacan

2022/5/26

#
if you can pass me the code
elvacan elvacan

2022/5/26

#
I would appreciate
danpost danpost

2022/5/29

#
elvacan wrote...
if you can pass me the code
With the int field:
private int timer = 0;
It might look something like this:
timer = (timer+1)%1800;
if (timer == 0)
{
    bar.setValue(bar.getValue()-1);
    if (bar.getValue() == 0) Greenfoot.stop();
}
You need to login to post a reply.