-
[RESOLVED] [F8] Is there a simpler way to code this?
Im making a flash RPG, and I want to display one "heart" mc for each HP the user has remaining. I have the following code that works atm, but i'm sure it could be put into a case statement or something that looks cleaner. Or maybe put the hearts in some kind of array, and loop through.
The user will eventually have 10 HP total. Any ideas on how to simplify this code in actioncript?
Thanks in advance
Code:
if (_root.user_one_hp = 1)
{
userheart_one._alpha = 100;
userheart_two._alpha = 0;
userheart_three._alpha = 0;
userheart_four._alpha = 0;
userheart_five._alpha = 0;
}
if (_root.user_one_hp = 1.5)
{
userheart_one._alpha = 100;
userheart_two._alpha = 50;
userheart_three._alpha = 0;
userheart_four._alpha = 0;
userheart_five._alpha = 0;
}
if (_root.user_one_hp = 2)
{
userheart_one._alpha = 100;
userheart_two._alpha = 100;
userheart_three._alpha = 0;
userheart_four._alpha = 0;
userheart_five._alpha = 0;
}
-
Maybe you can adapt this to what you are trying to do...
Code:
// array of userheart movie clips
var hearts:Array = [userheart_one, userheart_two, userheart_three, userheart_four, userheart_five];
// call this function whenever user_one_hp changes
function updateHearts() {
var val = user_one_hp;
for (i = hearts.length; i >= 0; i--) {
if (val >= i) {
var num = val - i;
if (num > 0) {
hearts[i]._alpha = num * 100;
val -= num;
}
hearts[i - 1]._alpha = 100;
val--;
} else {
hearts[i - 1]._alpha = 0;
}
}
}
HTH
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|