Solved

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

Posted on 2010-08-22
8
679 Views
Last Modified: 2013-12-26
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
Comment
Question by:truromeo4juliet
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:mattibutt
ID: 33495690
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
 
LVL 2

Assisted Solution

by:ghemstrom
ghemstrom earned 166 total points
ID: 33495735
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
 
LVL 5

Author Comment

by:truromeo4juliet
ID: 33495900
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
 
LVL 2

Expert Comment

by:ghemstrom
ID: 33495983
"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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Accepted Solution

by:
IqAndreas earned 334 total points
ID: 33496070
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
 
LVL 8

Assisted Solution

by:IqAndreas
IqAndreas earned 334 total points
ID: 33496131
@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
 
LVL 5

Author Comment

by:truromeo4juliet
ID: 33496318
@ 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
 
LVL 5

Author Closing Comment

by:truromeo4juliet
ID: 33497163
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now