Link to home
Start Free TrialLog in
Avatar of truromeo4juliet
truromeo4julietFlag for United States of America

asked on

Need help condensing this code, possibly using a 'for' statement

I need help streamlining this code (Actionscript 3.0)... I've got similar codes floating throughout my program and it's frustrating me to look at it, as I know there's a shorter way to write it (using a for statement and some small script that I'm not yet familiar with)... If I could get an example for condensing this using a 'for' statement (i believe that would be the best approach), I can then condense all the other lines that use this repetitive nonsense into something much more reader-friendly :)
if(parseInt(min_mc_string[0]) == 0)
		min_cartoon.item1.txt1.gotoAndStop(10);
	else if (isNaN(parseInt(min_mc_string[0])))
		min_cartoon.item1.txt1.gotoAndStop(11);
	else min_cartoon.item1.txt1.gotoAndStop(parseInt(min_mc_string[0]));
	
	if(parseInt(min_mc_string[1]) == 0)
		min_cartoon.item2.txt1.gotoAndStop(10);
	else if (isNaN(parseInt(min_mc_string[1])))
		min_cartoon.item2.txt1.gotoAndStop(11);
	else min_cartoon.item2.txt1.gotoAndStop(parseInt(min_mc_string[1]));

	if(parseInt(min_mc_string[2]) == 0)
		min_cartoon.item3.txt1.gotoAndStop(10);
	else if (isNaN(parseInt(min_mc_string[2])))
		min_cartoon.item3.txt1.gotoAndStop(11);
	else min_cartoon.item3.txt1.gotoAndStop(parseInt(min_mc_string[2]));

Open in new window

Avatar of mattibutt
mattibutt
Flag of United States of America image

you can't write using for because every single statement needs evaluation case can be used but again all these pre-conditions must be checked.
the behaviour also changes for each condition
SOLUTION
Avatar of ghemstrom
ghemstrom
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of truromeo4juliet

ASKER

Mattibutt - I get what you're saying, but the problem is that I'm imitating a functional code I've already seen implimented in AS2

Ghemstrom - I think you're on the right track, I actually came up with this on my own after doing a bit of research... I the code below would be the code I'm looking for, if it works correctly... if it works, it will run through the array elements one at a time and do a comparison until it reaches the end of the array... I'll try it when I get home:


for(var i:Number = 1, var j:Number = 0; j < min_mc_string.length+1; i++, j++){
	if(parseInt(min_mc_string[j]) == 0)
		min_cartoon.this["item"+i].txt1.gotoAndStop(10);
	else if (isNaN(parseInt(min_mc_string[j])))
		min_cartoon.this["item"+i].txt1.gotoAndStop(11);
	else min_cartoon.this["item"+i].txt1.gotoAndStop(parseInt(min_mc_string[j]));
}

Open in new window

"item"+i looks pretty strange to me even if I have no knowledge of AS. "item" is a string and i an integer variable. Does it really add up to an object identifier?
Can you use 'this' as an implied object identifier without pointing to the respective 'item' in the beginning of the loop?
 
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ lgAndreas - I was going to ask if there was something wrong with the code I wrote; I'm glad you pointed it out... I'm stuck at work for another 3.5 hours wanting to get home so I can employ this new technique! It condensed 72 lines of code into 12 :D

I really like what you did there to condense the "parseInt" lines into just "value"... it's WAY easier to read now! This is the code I've got now; will it work? And also, should there be a dot ( . ) between min_cartoon and ["item"+i] ?
for(var i:Number = 1, var j:Number = 0; j < min_mc_string.length+1; i++, j++){
	var value:Number = parseInt(min_mc_string[j]);
	if(value == 0) 		{ min_cartoon.["item"+i].txt1.gotoAndStop(10); }
	else if (isNaN(value))  { min_cartoon.["item"+i].txt1.gotoAndStop(11); }
	else 			{ min_cartoon.["item"+i].txt1.gotoAndStop(value); }
}

Open in new window

Got home and tested it... had a few issues but got them all sorted out... I don't need the dot before the ["item"+i] line, and only need one "var" in the "for" condition line... everything else worked beautifully and condensed a total of 82 lines into 11 lines of code :) Time to make these same changes throughout my program to make it smaller :D

Here's the end result if anyone's curious:

      for(var i:Number=1, j:Number=0; j < min_mc_string.length+1; i++, j++){
            var minvalue:Number = parseInt(min_mc_string[j]);
            var maxvalue:Number = parseInt(max_mc_string[j]);
            
            //Finds minimum hits and sends them to the animation
            if(minvalue == 0)                  min_cartoon["item"+i].txt1.gotoAndStop(10);
            else if (isNaN(minvalue))      min_cartoon["item"+i].txt1.gotoAndStop(11);
            else                                    min_cartoon["item"+i].txt1.gotoAndStop(minvalue);
            
            //Finds maximum hits and sends them to the animation
            if(maxvalue == 0)                  max_cartoon["item"+i].txt1.gotoAndStop(10);
            else if (isNaN(maxvalue))      max_cartoon["item"+i].txt1.gotoAndStop(11);
            else                                     max_cartoon["item"+i].txt1.gotoAndStop(maxvalue);
            
            //Plays the complete damage outputs
            min_cartoon["item"+i].gotoAndPlay(1);
            max_cartoon["item"+i].gotoAndPlay(1);
      }

the code was changed to include max value also... this is for a damage calculator I'm programming for Maple Story... see the progress here:

http://teamradftw.com/ms/phpBB3/viewtopic.php?f=2&t=19458

If anyone has any thoughts on how to further optimize my code, please PM me on my forum. Thanks guys!