View Full Version : [AS2] Health number help
keyspinner
12-19-2008, 12:40 AM
Ok so I am making a health counter for a simple rpg game in flash 8. I made the beginning health of player1 75 and I made a button that makes it take away 20 of the 75 and makes the number go down to 55 and so on. I made another button that added 25 but the problem is, instead of making 75 100 it made 75 become 7525. This is my first frame code, here are the button codes, also it would help if someone could tell me how to make the number stop going down once it reaches 0.
Frame1:
stop();
//current health
var health : Number = 75;
//max health
var maxHealth : Number = 100;
//healthbox
player1health.text=health;
Attack button:
on (release) {
_root.player1health.text -= 20;
}
Heal Button:
on (release) {
_root.player1health.text += 20;
}
atomic
12-19-2008, 12:56 AM
on (release) {
if(Number(_root.player1health.text) > 0){
_root.player1health.text -= 20;
}else{
_root.player1health.text = 0;
}
}
keyspinner
12-19-2008, 12:43 PM
Ok, I understand how that makes the hp go to zero once it reaches to it, but it makes it -5 and then zero. Anyway I can make it go straight to zero instead of going into the negatives, and I also need help with the addition problem I am having.
pradvan
12-19-2008, 02:50 PM
I think you just need to store your player health in a dynamic textbox var, instead of a dynamic textbox instance. Then use this code:
player1health -= 20;
player1health += 20;
etc
atomic
12-19-2008, 06:08 PM
on (release) {
if(Number(_root.player1health.text) > 20){
_root.player1health.text -= 20;
}else{
_root.player1health.text = 0;
}
}
The text is a string, so addition is treated as concatination. I forget how this is handled in AS2.
Number(_root.player1health.text) + 20
pradvan
12-19-2008, 08:33 PM
The text is a string, so addition is treated as concatination. I forget how this is handled in AS2.
Number(_root.player1health.text) + 20
So you're basically saying
player health = string, player health = number
just lose the .text
From context, I took player1health to be a TextField.
atomic
12-19-2008, 10:54 PM
:rolleyes:
@ keyspinner...
If it was working for you with my first script, just change it acoording to my post #5...
keyspinner
12-20-2008, 12:38 AM
Uh, now that I have changed it to
on (release) {
_root.player1health += 20
}
the buttons don't even do anything
Atomic, you are talking about something completely different...
I am talking about addition the first script made it go into negative five, and you helped me with that in the second script, now I am talking about adding.
atomic
12-20-2008, 12:45 AM
Anyway I can make it go straight to zero instead of going into the negatives...
If so what's the above supposed to mean?
atomic
12-20-2008, 12:48 AM
on (release) {
_root.player1health.text = Number(_root.player1health.text)+20;
}
ldb358
12-20-2008, 09:05 PM
okay wait are you try to make the hitpoints count down from say 100 and stop at 0?
atomic
12-20-2008, 09:10 PM
No. It's about adding!
FluffyPapes
12-24-2008, 06:05 PM
Here's something I do in my game, but I'm too lazy to actually post the AS code.
So, I set the hp value to a variable like
var health = _root.playerhealth;
So, then I take the damage of the monster and I set an if statement:
This is pseudocode, by the way:
if((health - monsterDamage) <= 0)
{
health = 0;
}else{
health -= monsterDamage;
}
Forgot a god-forsaken semicolon.
orange gold
12-24-2008, 06:17 PM
the thing is because its a text feild.. i dunno why adobe/macromedia did this but - subtracts and + adds to orriginal (non mathematically) inorder to get this right all you have to do is simple algebra
you can add or subtract a negative
subtracting a negative is the same as adding
so
instead of
player1health.text += 20;
you would have to put
player1health.text - = -20;
FluffyPapes
12-24-2008, 06:20 PM
the thing is because its a text feild.. i dunno why adobe/macromedia did this but - subtracts and + adds to orriginal (non mathematically) inorder to get this right all you have to do is simple algebra
you can add or subtract a negative
subtracting a negative is the same as adding
so
instead of
player1health.text += 20;
you would have to put
player1health.text - = -20;
I never had to do that. It works fine with me.
Then again, I never was able to do the +=, it gave me an error so I typed it out and it worked fine.
I'd prefer typing it out because -= -20 looks confusing.
orange gold
12-24-2008, 06:32 PM
the thing is because its a text feild.. i dunno why adobe/macromedia did this but - subtracts and + adds to orriginal (non mathematically) inorder to get this right all you have to do is simple algebra
you can add or subtract a negative
subtracting a negative is the same as adding
so
instead of
player1health.text += 20;
you would have to put
player1health.text - = -20;
it really isnt that confusing all it is saying is subtract negative 20 which also means add 20. writing it out takes up more space on the final file and scence rpg is all about memory you want to use as less lines of code and as possible to keep everything short and neat and easy to edit and to keep the file size down
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.