AS 2.0 Dynamic TextBox Problems

Started by: Plague | Replies: 9 | Views: 897

Plague

Posts: 415
Joined: Feb 2007
Rep: 10

View Profile
Apr 22, 2009 3:00 AM #403321
Okay, I am making a game. I am working on the health bar.
I am using percentage and have no clue as to why my code won't work.

I have searched google, many many flash communities with documentation, and tuts on SP. And nothing helps. It is such a simple problem, but yet it is very stuborn.

I have a dynamic text field with a var name of health.
here is the code I am using to decrease it.


function doDamage(damage:Number) {
if (_root.health != Number(0)) {
_root.health -= damage; // Returns NaN
}else{
die();
}
refreshIndicator();
}


All it does is return "NaN".
When I do the following though (outside of this function):

_root.health = 10; // Works
_root.health = Number(10); // Works
_root.health -= 10; // Doesn't
_root.health = _root.health - 10; // Doesn't
_root.health = _root.health - Number(10);// Doesn't
_root.health = Number(_root.health - 10);// Doesn't


Can someone help me figure out what operations to use to decrease the number in the dynamic textbox?
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Apr 22, 2009 3:34 AM #403343

function doDamage(damage:Number) {
if (Number(_root.health) != 0) {
_root.health = Number(_root.health) - damage;
}else{
die();
}
refreshIndicator();
}


You had it backwards. _root.health is being turned into text, so you want to convert it to a number before doing operations on it.

Really though, using linked variables with dynamic textfields is more trouble than it's worth. You would be better off creating a variable and casting it as a number, and then manually updating textfield.text whenever necessary.

Also, you need to consider the possibility that their health will be negative.
Plague

Posts: 415
Joined: Feb 2007
Rep: 10

View Profile
Apr 22, 2009 1:50 PM #403556
It still returns NaN.
Also, I have considered the possibility that it is a negitive.
This is the only method I use to subtract health, and only by 10 percent each time.

...

Wait, I see what you mean about the negatives.

I have improved the code, but it still returns NaN.

function doDamage(damage:Number) {
_root.health = Number(_root.health) - damage;
if (Number(_root.health) <= 0) {
die(); // Subract a life, restart level, check if game is over.
}
refreshIndicator();
}

// I have also tried:

var _health:Number = Number(_root.health) - damage;
_root.health.text = _health;
Wtf
2

Posts: 5,683
Joined: Oct 2006
Rep: 10

View Profile
Apr 22, 2009 2:11 PM #403575
_root.health = Number(_root.health) - damage;

I might be getting it wrong, but are you saying that the current number of HP is equal to the damage taken?
That would make the AS understand that when HP = x Damage = x
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Apr 22, 2009 2:20 PM #403582
How are you setting the initial value of _root.health? Are you just entering it as text into the textfield? You mentioned percents, did you put a percent sign after it?

I tested it, and here's what worked/didn't for me:

textfield left blank, "_root.health=100" in first frame - works
textfield has text "100" - works
textfield has text "100%" - does not work
textfield left blank, _root.health not set in first frame - does not work

If you did the third one, just put the "%" in a seperate textfield (static)
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Apr 22, 2009 2:24 PM #403585
Quote from Wtf
_root.health = Number(_root.health) - damage;

I might be getting it wrong, but are you saying that the current number of HP is equal to the damage taken?
That would make the AS understand that when HP = x Damage = x


No, the Number() function is used to force Flash to convert the variable to a number. So if a variable was actually a string (text) that held the number "34", you would use Number("34") to turn it into 34. Because you can't do mathematical operation on text, you have to convert it to a number first.

That snippet is telling Flash that _root.health should be set equal to the Numerical value of _root.health minus the damage done. If there wasn't any chance that _root.health would hold text instead of a number, it would be written as _root.health-=damage
Plague

Posts: 415
Joined: Feb 2007
Rep: 10

View Profile
Apr 22, 2009 2:26 PM #403588
Quote from darkcampainger
How are you setting the initial value of _root.health? Are you just entering it as text into the textfield? You mentioned percents, did you put a percent sign after it?

I tested it, and here's what worked/didn't for me:

textfield left blank, "_root.health=100" in first frame - works
textfield has text "100" - works
textfield has text "100%" - does not work
textfield left blank, _root.health not set in first frame - does not work

If you did the third one, just put the "%" in a seperate textfield (static)


I put the % as a static, yes.
And, yes, the initial value is "100" typed in the dynamic textbox.
And Then I set _health to the value of Number(_root.health), which may or may not still equal 100 depending on whether or not _root.health has been changed already by this method. Although, slim chance considering it doesn't work in the first place.
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Apr 22, 2009 2:33 PM #403593
Quote from killdude69
I put the % as a static, yes.
And, yes, the initial value is "100" typed in the dynamic textbox.
And Then I set _health to the value of Number(_root.health), which may or may not still equal 100 depending on whether or not _root.health has been changed already by this method. Although, slim chance considering it doesn't work in the first place.


Well, this is working just fine for me. Remove all text from the dynamic textfield, and put this on the root timeline (I assume it was originally there, if it is under the player movieclip or something, you'll have to add the '_root.'s back in)


var health:Number = 100;

function doDamage(damage:Number) {
health-=damage;
if (health <= 0) {
die(); // Subract a life, restart level, check if game is over.
}
}


If that still doesn't work, check the areas where you are calling doDamage and make sure proper values are being passed.
Plague

Posts: 415
Joined: Feb 2007
Rep: 10

View Profile
Apr 22, 2009 2:45 PM #403599
Ok, I got it to work. thanks
Wtf
2

Posts: 5,683
Joined: Oct 2006
Rep: 10

View Profile
Apr 22, 2009 4:49 PM #403631
Quote from darkcampainger
No, the Number() function is used to force Flash to convert the variable to a number. So if a variable was actually a string (text) that held the number "34", you would use Number("34") to turn it into 34. Because you can't do mathematical operation on text, you have to convert it to a number first.

That snippet is telling Flash that _root.health should be set equal to the Numerical value of _root.health minus the damage done. If there wasn't any chance that _root.health would hold text instead of a number, it would be written as _root.health-=damage

Got it.
Thanks for the tip.
I used to be in touch with AS but then I stooped and now i barely recall anything.