Hello, I'm new here, but I've been trying to get a sprint stamina system to work for a while now. I don't think I'm going about it the correct way.

What I've been trying is to create a boolean method that returns true while the meter isn't 0. What I'm having issues coming up w/ is how to design the method to count down and where to call it. I am currently trying to call the boolean method (sprintStamina()) in an if statement inside of my update method where my movement code is, like this:

1 2 3 4 5

if (input.run && input.right) {if (sprintStamina()) {xa = xa + 1; }}

and my sprintStamina() method is where I'm having trouble coming up w/ the correct code to accomplish this. My sprintStamina() method is in shambles and very broken. I'm having trouble figuring this problem out. If anyone can help me, I'd appreciate it. If you need more detail as to what I'm trying to do, please ask.

First of all, if you boolean method returns your stamina value, shouldn't you name it more adequately? like getStamina() or hasStamina() ?

And you can set up a void method changeStamina(int value) to account for stamina changes (like grabbing some kind of stamina pill to refill it, or deplete it by running, or whatever magic you want to do)

I would like to add that usually in a game cycle every time based event (things working over an arbitrary amount of time like slow movement from one point to another or fading out...) needs to have a momentary calculated event state. In order to do the calculation, the amount of time that has been spend since the last frame is needed. This is often called "delta".

What that means for you: if you want to know how much stamina is used in one single frame, you have to take the time between the last frame and the current frame. Then you need to multiply this (maybe about 10 milliseconds) with a value of your choice that fits into the equation: staminaLost = milliseconds * staminaConsumptionPerMS

You need a similar equation for restoring the stamina on a time base, if this is what you like to do.

and in my main Game class I added a few lines to my run() method: To get it to update once every frame was rendered I figured it would need to be called from here so I initialized a new regenTimer and had it add to the stamina every half second in increments of 2. I bolded the lines that got the program working correctly for me.

if (anim < 7500) { anim++; }else{anim = 0; } // -- if you don't have the stamina, don't bother checking movement inputs. if(hasStamina()){// -- check either up or down since you can move up and down at the same time.if (input.up){ya--; }elseif (input.down){ya++; }// -- check either left or right since you can't move left and right at the same time. if (input.left){xa--; }elseif (input.right){xa++; }

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org