dcsimg
A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: How to write efficient actionscript? Any tips?

  1. #1
    Senior Member
    Join Date
    Mar 2007
    Posts
    133

    How to write efficient actionscript? Any tips?

    Hey guys.

    I've been using flash for a good 7-8 months now, but i've always had a bit of a problem with writing efficient actionscript. Basically at the moment im just using a bunch of if else statements to execute all the "in" and "out" transitions of the site.

    For example, (AS2.0)

    aboutHit_mc.onRelease = function () {

    if(currentPage != "about"){

    //remove contents of current page
    if(currentPage == "home"){
    //use mask to hide contents of homepage
    extAnimMask_mc.visible = true;
    extAnimMask_mc.gotoAndPlay("_in");
    box1Hit_mc.gotoAndStop("_off");
    box2Hit_mc.gotoAndStop("_off");
    box3Hit_mc.gotoAndStop("_off");
    box4Hit_mc.gotoAndStop("_off");
    contactU****_mc.gotoAndStop("_off");
    requestAQuoteHit_mc.gotoAndStop("_off");

    } else if(currentPage == "services"){
    //close services page
    servicesLoader_mc.extAnimMask_mc.gotoAndPlay("_out ");
    services_mc.gotoAndPlay("_out");
    } else if(currentPage == "portfolio"){
    portfolioLoader_mc.extAnimMask_mc.gotoAndPlay("_ou t");
    portfolioLoader_mc.navMask_mc.gotoAndPlay("_out");
    portfolio_mc.gotoAndPlay("_out");
    } else if(currentPage == "downloads"){
    downloadsLoader_mc.extAnimMask_mc.gotoAndPlay("_ou t");
    downloads_mc.gotoAndPlay("_out");
    } else if(currentPage == "quote"){
    quoteLoader_mc.extAnimMask_mc.gotoAndPlay("_out");
    quote_mc.gotoAndPlay("_out");
    } else if(currentPage == "contact"){
    contactLoader_mc.extAnimMask_mc.gotoAndPlay("_out" );
    contact_mc.gotoAndPlay("_out");
    }

    //load new page
    mcLoader.loadClip("about.swf", aboutLoader_mc);
    currentPage = "about";
    //highlight about text on nav
    about_mc.gotoAndStop(7);
    extAnimMask_mc.visible = false;
    }
    }



    This style or programming is making a lot of my work run quite slowly, so im keen to hear any advice on how to improve this. Im going to buy a couple of books on AS3 but for the next month or two im staying with AS2.

    Any help would be greatly appreciated.

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    if you name your labels the same than the currentPage var, you would not even need if statements.
    for example, you could replace:
    Code:
    if (currentPage == "home") {
    	box1Hit_mc.gotoAndStop("_off");
    	box2Hit_mc.gotoAndStop("_off");
    	box3Hit_mc.gotoAndStop("_off");
    	box4Hit_mc.gotoAndStop("_off");
    }
    with:
    Code:
    for (i=1; i<=4; i++) {
    	this["box"+i+"Hit_mc"].gotoAndStop(currentPage);
    }
    just by changing "_off" to "home"

    for loops also help reducing the Number of lines, when dealing with serial named instances.

    gparis

  3. #3
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Thx gparis. So basically less code usually equals more efficient code? Do you think i should go back and rewrite all my code to fit in with your example? Would the performance savings be worth this given that my code is about 870 lines long atm? Im really keen to boost the frame rate of the site.

    Also, does anybody else have any comments to make/links about actionscript efficiency?

  4. #4
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    try using symbols like || and && or ** in between your state ments ill use
    if(this.hitTest(_root.thing)) you can do this if(this.hitTest(_root.thing || _root.thing1)) that will be if it hits thing or thing1 the && is and and the ** is niether nor, meaning if this doesnt hit any of them

  5. #5
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    i can't seem to get the || and && conditional operators to work for me when im testing the current page variable. Any other tips i could use here?

  6. #6
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    uhh sorry i doesnt seem to work for me either , i know these are operators and if i figure out how to use these ill tell you
    Signature Goes Here

  7. #7
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Quote Originally Posted by chur
    Thx gparis. So basically less code usually equals more efficient code? Do you think i should go back and rewrite all my code to fit in with your example? Would the performance savings be worth this given that my code is about 870 lines long atm? Im really keen to boost the frame rate of the site.

    Also, does anybody else have any comments to make/links about actionscript efficiency?
    Yes. you should definitely re-write.

    for a search on this topic, either here or at adobe, use the keyword 'optimise'

    gparis

  8. #8
    Senior Member
    Join Date
    Nov 2005
    Location
    dante's inferno
    Posts
    904
    to get this to work, you would write it like this:

    if(this.hitTest(_root.thing) || this.hitTest( _root.thing1));

    you have to write out the whole statement again:

    if (apples == 'green' || apples == 'red'){
    //do this
    }
    if (apples == 'red' && type == 'Macintosh'){
    trace('these are good');
    }



    IMS

  9. #9
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    You could further streamline this code by using the variable "currentPage" to point directly to the clip:

    code:

    this[currentPage + "Loader_mc"].extAnimMask_mc.gotoAndPlay("_out");



    For instance, if currentPage == 'quote', this would evaluate to

    code:

    quoteLoader_mc.extAnimMask_mc.gotoAndPlay("_out");



    Make any sense?
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  10. #10
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Sweet, thx for the tips guys. Im going to try them out today.

  11. #11
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Ok i managed to drastically optimise the code (thx guys ), but now im getting compiler errors. Does anyone know what's wrong with my syntax?

    aboutHit_mc.onRelease = function () {
    if(currentPage != "about"){
    if(currentPage == "home"){
    removeHomepage();
    } else {
    this[currentPage + "Loader_mc"].extAnimMask_mc.gotoAndPlay("_out");
    this[currentPage + "_mc"].gotoAndPlay("_out");
    }
    //load new page
    currentPage = "about";
    loadExternalSwf(currentPage);
    }
    }

    function loadExternalSwf(var page:String):Void { //error parameter name expected?
    mcLoader.loadClip([page + ".swf"],[page + "Loader_mc"]);
    this[page + "_mc"].gotoAndStop(7);
    extAnimMask_mc.visible = false;
    } //Unexpected '}' encountered

    function removeHomepage():Void {
    extAnimMask_mc.visible = true;
    extAnimMask_mc.gotoAndPlay("_in");
    contactU****_mc.gotoAndStop("_off");
    requestAQuoteHit_mc.gotoAndStop("_off");

    for (var i=1; i<=4; i++) {
    this["box"+i+"Hit_mc"].gotoAndStop("_off");
    }
    }


    Thx

  12. #12
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    change:
    function loadExternalSwf(var page:String):Void {
    to:
    function loadExternalSwf(page:String):Void {

    flash doesn't let you declare variables inside of function parameter fields. Declare the variable above all the code if you're feeling frisky, otherwise let it be.
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  13. #13
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Cheers mneil. It compiles sweet now. Only one more issue... I can't get the red line of code to load the external about.swf into the aboutLoader_mc movieclip. Everything else works sweet though.

    function loadExternalSwf(page:String):Void {
    mcLoader.loadClip(this[page + ".swf"],this[page + "Loader_mc"]);
    this[page + "_mc"].gotoAndStop(7);
    extAnimMask_mc.visible = false;
    }

    function removeHomepage():Void {
    extAnimMask_mc.visible = true;
    extAnimMask_mc.gotoAndPlay("_in");
    contactU****_mc.gotoAndStop("_off");
    requestAQuoteHit_mc.gotoAndStop("_off");

    for (var i=1; i<=4; i++) {
    this["box"+i+"Hit_mc"].gotoAndStop("_off");
    }
    }

    aboutHit_mc.onRelease = function () {
    if(currentPage != "about"){
    if(currentPage == "home"){
    removeHomepage();
    } else {
    this[currentPage + "Loader_mc"].extAnimMask_mc.gotoAndPlay("_out");
    this[currentPage + "_mc"].gotoAndPlay("_out");
    }
    //load new page
    currentPage = "about";
    loadExternalSwf(currentPage);
    }
    }


    Any ideas why the loadClip function won't accept my input?

  14. #14
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    you're using this in your loadExternalSwf which is refering to aboutHit_mc. So, unless aboutLoader_mc is located inside of aboutHit_mc, you'll need to change this to _root. Or, get rid of the [] and this all together on that line, and the next line you'll need to use root or else you'll get an error again. Because, right now what your saying is:

    mcLoader.loadClip(aboutHit_mc[about.swf], aboutHit_mc[aboutLoader_mc]);

    See what I'm saying?


    It's usually a good practice to avoid _root, and there are ways to get around using it, but for this purpose using _root will most likely not cause any problems for you.
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  15. #15
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Thx man. I removed the [] and it works perfect .

  16. #16
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Managed to decrease the code from 870 lines to 424 lines whilst keeping the same line spacing for both. Very happy with that. Need to optimise the external swf's now.

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