A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] [F8] Is there a simpler way to code this?

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    18

    resolved [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;
    	}

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518

    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

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    18

    Talking golly thats amazing

    Nice, works perfectly.

    Thanks for the time and the effort It's greatly appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center