Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Sprint Stamina  (Read 865 times)
0 Members and 1 Guest are viewing this topic.
Offline kevlam585

Junior Newbie





« Posted 2013-01-15 15:14:14 »

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.
Offline Rorkien
« Reply #1 - Posted 2013-01-15 16:14:45 »

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)

1  
2  
3  
4  
5  
6  
7  
8  
if (/*Im pressing run, ho ho!*/) {
   //Hey, do i have enough stamina to run?
   if(hasStamina()) {
      //I DO! YAY!
      changeStamina(/*Stamina deducted for running*/);
      xa = xa + 1; //whatever this does... o_o
   }
}
Offline Sparky83
« Reply #2 - Posted 2013-01-15 16:30:37 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevlam585

Junior Newbie





« Reply #3 - Posted 2013-01-15 21:03:54 »

This may not be entirely correct in the way someone with more experience than I would do it, but I got it to work.

Here's what I did in my Player class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
public void update() {
   int xa = 0, ya = 0;
   if (anim < 7500) {
      anim++;
   } else {
      anim = 0;
   }
   if (input.up) ya--;
   if (input.down) ya++;
   if (input.left) xa--;
   if (input.right) xa++;
   if (input.run && input.right) {
      if (hasStamina()) {
         xa = xa + 1;
         changeStamina(1);
      }
   }
   if (input.run && input.left) {
      if (hasStamina()) {
         xa = xa - 1;
         changeStamina(1);
      }
   }
   if (input.run && input.up) {
      if (hasStamina()) {
         ya = ya - 1;
         changeStamina(1);
      }
   }
   if (input.run && input.down) {
      if (hasStamina()) {
         ya = ya + 1;
         changeStamina(1);
      }
   }
   if (xa != 0 || ya != 0) {
      move(xa, ya);
      walking = true;
   } else {
      walking = false;
   }
}
   
public int getStamina() {
   return stamina;
}

private boolean hasStamina() {
   if (stamina > 0) {
      return true;
   } else {
      return false;
   }
}

public void changeStamina(int changeAmt) {
   stamina -= changeAmt;
   System.out.println(stamina);
}


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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public void run() {
   long lastTime = System.nanoTime();
   long timer = System.currentTimeMillis();
   long regenTimer = System.currentTimeMillis(); //The new regenTimer I initialized
   final double ns = 1000000000.0 / 60.0;
   double delta = 0;
   int frames = 0;
   int updates = 0;
   requestFocus();
   while (running) {
      long now = System.nanoTime();
      delta += (now - lastTime) / ns;
      lastTime = now;
      while (delta >= 1) {
         update();
         updates++;
         delta--;
      }
      render();
      frames++;

      if (System.currentTimeMillis() - timer > 1000) {
         timer += 1000;
         frame.setTitle(title + " | " + updates + " ups, " + frames + " fps");
         frames = 0;
         updates = 0;
      }

                //Added this part for the regeneration of Stamina
      if (System.currentTimeMillis() - regenTimer > 500) {
         regenTimer += 500;
         if (player.getStamina() < 100) {
            player.changeStamina(-2);
         }
      }
   }
   stop();
}


This might not be the best way for this, but I got it working, so I'm glad for that. What would be a better method to do this?
Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #4 - Posted 2013-01-18 18:38:37 »

Just as a suggestion for optimization purposes... I would check to see if you have stamina first before even checking the inputs...since if you don't have stamina, your character can't move anyways.

secondly, I would put in "else if" statements since you can't run both left and right, and up and down... If you don't mind, I restructured it a bit Smiley

Please Note: I wrote this in notepad since I don't have an editor here, so there's a very real possiblity that there's a syntax error lol.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
public void update() {   
  int xa = 0, ya = 0;  

  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--;  
  }else if (input.down){
    ya++;
  }
  // -- check either left or right since you can't move left and right at the same time.  
  if (input.left){
    xa--;
  }else if (input.right){
    xa++;
  }

  if (input.run && input.right){    
   xa = xa + 1;        
   changeStamina(1);
  }else if (input.run && input.left) {        
   xa = xa - 1;        
   changeStamina(1);
  }  

  if (input.run && input.up){          
   ya = ya - 1;        
   changeStamina(1);  
  }else if (input.run && input.down) {
   ya = ya + 1;
   changeStamina(1);
  }
}

if (xa != 0 || ya != 0) {
    move(xa, ya);      
    walking = true;  
} else {      
    walking = false;  
}

}




-Pickle

Offline Rorkien
« Reply #5 - Posted 2013-01-18 18:52:39 »

If it works, and you know HOW it works, it's all good. You can always refactor everything later, once you get a good grab of the code.
Offline kevlam585

Junior Newbie





« Reply #6 - Posted 2013-01-18 19:06:25 »

Thanks for the help guys!! I got it working to my liking and added some of your suggestions into it. I appreciate all of your time!
Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #7 - Posted 2013-01-18 19:45:14 »

If it works, and you know HOW it works, it's all good. You can always refactor everything later, once you get a good grab of the code.

Rorkien is 100% right... disregard what I posted previously Smiley


-Pickle

Offline Rorkien
« Reply #8 - Posted 2013-01-18 19:59:13 »

If it works, and you know HOW it works, it's all good. You can always refactor everything later, once you get a good grab of the code.

Rorkien is 100% right... disregard what I posted previously Smiley


-Pickle

Don't! What i said actually never worked for me.
I'm the kind of person that gets depressed when he knows some piece of code isn't on the way he likes...  Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!