Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

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

0
truromeo4juliet
Asked:
truromeo4juliet
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
mattibuttCommented:
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
0
 
ghemstromCommented:
I have never seen Activescript before but if it is possible to define a vector containing min_cartoon.item1 ... min_cartoon.item3 you could step through the code like this

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

Open in new window

0
 
truromeo4julietAuthor Commented:
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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ghemstromCommented:
"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?
 
0
 
IqAndreasCommented:
ghemstrom is on the right track, but contains a few slight typos, yet I'm really surprised how well the code is written for writing it without any previous ActionScript experience. :)

Try the attached code (where I also define the Array, which should preferably done outside of the function that you use to go to different frames. Also, I moved some of the code onto single lines to make it a bit more readable, but it is definitely possible to move those down onto the next line.

Just ask if you have any more questions regarding the code or any other ActionScript,
Andreas
var itemsArray:Array = new Array();
itemsArray[0] = min_cartoon.item1.txt1;
itemsArray[1] = min_cartoon.item2.txt1;
itemsArray[2] = min_cartoon.item3.txt1;

for (var i:int = 0; i < itemsArray.length; i++)
{
    var value:Number = parseInt(min_mc_string[i]);
    if (isNaN(value))    { itemsArray[i].gotoAndStop(11); }
    else if (value == 0) { itemsArray[i].gotoAndStop(10); }
    else                 { itemsArray[i].gotoAndStop(value); }    
}

Open in new window

0
 
IqAndreasCommented:
@trueromeo4juliet - Actually, that code also almost works, but contains a few slight errors.

It is not possible to access "someMovieClip.this...", so this line wouldn't work:
min_cartoon.this["item"+i].txt1.gotoAndStop(10);

It would have worked without the "min_cartoon" part, but then you are referencing an entirely wrong MovieClip.

Instead, you can do like this (removing the 'this', and accessing the "item plus a number" property from min_cartoon instead:
min_cartoon["item"+i].txt1.gotoAndStop(10);

Sorry that ActionScript seems so convoluted and backwards everywhere, but a large part has to do with the Flash IDE doing so many things automatically, that the logic behind some parts never becomes clear. :P But don't worry, the more you work with ActionScript, the easier it gets.

Just ask if you still have any questions,
Andreas
0
 
truromeo4julietAuthor Commented:
@ 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

0
 
truromeo4julietAuthor Commented:
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!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now