Be a Supporter!

Hp maximum

  • 323 Views
  • 8 Replies
New Topic Respond to this Topic
piggyclock
piggyclock
  • Member since: Dec. 14, 2005
  • Offline.
Forum Stats
Member
Level 54
Programmer
Hp maximum 2008-08-27 07:38:13 Reply

im almost done with a game im making,i need to know how to keep a hp level from going over the maximum,any ideas

colintso
colintso
  • Member since: Aug. 22, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Hp maximum 2008-08-27 07:56:59 Reply

Your hp has a value right?
So,

if hp>=(your max hp) {
hp==(your max hp)
}

simple?

Cojones893
Cojones893
  • Member since: Mar. 9, 2003
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to Hp maximum 2008-08-27 10:59:17 Reply

At 8/27/08 07:56 AM, colintso wrote: Your hp has a value right?
So,

if hp>=(your max hp) {
hp==(your max hp)
}

simple?

Wow lets clean that up a bit so it's actually helpful and correct.

var hp:Number = 100;
var maxHP:Number = 100;
if(hp>=maxHP){
   hp=maxHP;
}

remember to check that everytime you add health to the player.

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Hp maximum 2008-08-27 11:51:40 Reply

At 8/27/08 10:59 AM, Cojones893 wrote:
At 8/27/08 07:56 AM, colintso wrote: Your hp has a value right?
So,

if hp>=(your max hp) {
hp==(your max hp)
}

simple?
Wow lets clean that up a bit so it's actually helpful and correct.

var hp:Number = 100;
var maxHP:Number = 100;
if(hp>=maxHP){
hp=maxHP;
}

remember to check that everytime you add health to the player.

I assume he won't need to declare HP and it's already in a variable.

Also, he doesn't HAVE to have a maxHP var, but you're definatly correct in saying he should


- Matt, Rustyarcade.com

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to Hp maximum 2008-08-27 12:27:51 Reply

At 8/27/08 11:51 AM, Rustygames wrote:
At 8/27/08 10:59 AM, Cojones893 wrote:
At 8/27/08 07:56 AM, colintso wrote: Your hp has a value right?
So,

if hp>=(your max hp) {
hp==(your max hp)
}

simple?
Wow lets clean that up a bit so it's actually helpful and correct.

var hp:Number = 100;
var maxHP:Number = 100;
if(hp>=maxHP){
hp=maxHP;
}

remember to check that everytime you add health to the player.
I assume he won't need to declare HP and it's already in a variable.

Also, he doesn't HAVE to have a maxHP var, but you're definatly correct in saying he should

Agreed. A better version of this code, though, would be using a while() loop, to avoid the one frame in which you can see the hp above the maximum that you get otherwise:

var hp:Number = 100;
var maxHP:Number = 100;
while(hp>maxHP){
   hp=maxHP;
}

;D


Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Hp maximum 2008-08-27 14:52:15 Reply

That's just not true. Just update the text field after you do that operation. I don't see how that while loop will solve anything...


- Matt, Rustyarcade.com

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to Hp maximum 2008-08-27 15:20:03 Reply

At 8/27/08 02:52 PM, Rustygames wrote: That's just not true. Just update the text field after you do that operation. I don't see how that while loop will solve anything...

EnterFrame works every frame. When I use that code, I get a single frame in which the health is above the maximum, which, of course, flashes and then vanishes. It looks unprofessional. The while loop runs it more than one a frame, so the number is reset before the next frame, which allows for a smoother look.


Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

BBS Signature
Ben-Fox
Ben-Fox
  • Member since: Dec. 27, 2003
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Hp maximum 2008-08-27 15:49:22 Reply

Easiest way of all avoids all if statements and while loops:

currentHP = Math.min(currentHP, maxHP)

Or, to incorporate that directly into a healing routine:

currentHP = Math.min(currentHP + healAmount, maxHP);

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Hp maximum 2008-08-27 16:04:44 Reply

At 8/27/08 03:20 PM, Hoeloe wrote:
At 8/27/08 02:52 PM, Rustygames wrote: That's just not true. Just update the text field after you do that operation. I don't see how that while loop will solve anything...
EnterFrame works every frame. When I use that code, I get a single frame in which the health is above the maximum, which, of course, flashes and then vanishes. It looks unprofessional. The while loop runs it more than one a frame, so the number is reset before the next frame, which allows for a smoother look.

I think you've completely misunderstood what the while loop does... The display is only updated every frame. So your code does exactly the same as an if...

Also, I assume you are just setting a dynamic text fields "var" property to hp, you shouldn't be doing this, just update it after all operations are done like so:

hp_txt.text = hp;

At 8/27/08 03:49 PM, Ben-Fox wrote: Easiest way of all avoids all if statements and while loops:

currentHP = Math.min(currentHP, maxHP)

Or, to incorporate that directly into a healing routine:

currentHP = Math.min(currentHP + healAmount, maxHP);

That's actually much better, use this method


- Matt, Rustyarcade.com