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

2013/6/10

Changing a progress meter

BubbaB BubbaB

2013/6/10

#
So I have a "recharge" meter in my game that visually lets the player know how much the gun has recharged. I wrote this method. But I need help because for some reason the image for the meter stays at the default image and won't change. (Rocket.maxRecharge is a final int set to 200, and Rocket.recharge is an in that is constantly increasing and resets to zero when the spacebar is pushed.) public void changeMeter() { for(int i = 0; i < 7; i++) { if((Rocket.maxRecharge /6) * i-1 < Rocket.recharge && Rocket.recharge < (Rocket.maxRecharge / 6) * i) { setImage("chargemeter" + i + ".png"); scaleImage(75, 100); } } Thanks!
davmac davmac

2013/6/10

#
Multiplication binds tighter than subtraction. You could fix the problem by changing the "if" statement to:
if((Rocket.maxRecharge /6) * (i-1) < Rocket.recharge && Rocket.recharge < (Rocket.maxRecharge / 6) * i)
(However, that's still quite inefficient. How about just multiply recharge by 6 and then divide it by maxRecharge, and use that as the charge meter index value? you don't need a for loop here!)
BubbaB BubbaB

2013/6/10

#
Yes thank you! My brain tends to over-think things.
You need to login to post a reply.