PDA

View Full Version : Optimizing code using Var


theskyisblue
12-08-2007, 04:54 PM
Hi,
In trying to reduce the amount of repeated code i believe that i shoud be using var. However, how can i split up a URL which is in quotation marks so that the bulk of the code can be called every time a different picture is required?

loadMovie("http://really_long_url/fish.jpg", _root.picholdr.piccys);

I am thinking along these lines:
loadMovie(url+"golf.jpg"._root.maskee.golf)
and
var url = "http://really_long_url/"

but perhaps there is an even shorter way - calling the loadmovie, and destination movie when requred too?


I also have another chunk of code repeated throughout my movie, though i am having difficulty placing the transition import code anywhere other than where the rest of the code is. How can i declare the transitions only once at the beginning of the movie?

import mx.transitions.Tween;
import mx.transitions.easing.*;
var myHoriTween:Tween = new Tween (golf,"_xscale",Elastic.easeOut,105,100,2,true);
var myHoriTween:Tween = new Tween (golf,"_yscale",Elastic.easeOut,105,100,2,true);

Many Thanks

Malc
12-08-2007, 05:43 PM
Hi,

What's wrong with the code that you are using; it seem pretty straight forward and condensed to me:

var url:String = "http://really_long_url/";
loadMovie(url + "golf.jpg", _root.maskee.golf);

If the file directory remains constant throughout your application then you could obviously use a function to cut down on the code required to fire the loading functionality:

var url:String = "http://really_long_url/";

function loadTheMovie(movie:String, fileExt:String, targetMC:MovieClip):Void {
loadMovie(url + movie + fileExt, targetMC);
}

loadTheMovie("golf", "jpg", _root.maskee.golf);

Your use of the '_root' level, and your second question, leads me to believe that you have your code fragmented throughout your application: it is not all in one place. If this is the case, I would suggest that you try to keep your code all on the main timeline and, if possible, all in one keyframe.

This now leads me onto your second question: the continual declaration of the imported classes. As far as I'm aware, there is no getting away with it; if you are about to utilise a class, e.g. Tween, you must declare it in the same keyframe in which you intend to use it. Therefore, your continual typing of this code IS necessary if your code is in more than one place. By adopting the approach that I mentioned above, where you try to keep all of your code in one place, means that you will only require one declaration.

Hope this helps,


Malc.

theskyisblue
12-08-2007, 06:55 PM
Malc, thanks... great help!
Easy when you know how isnt it - very useful to be understand that var implementation now.
The reason i was so concerned about the lengthy url is that i had to shave off 1-2kb (nothing i know, but vital for this project).

Regarding those transitions, i'll have a go at including the transitions as a variable. The trouble is i have at least four buttons which call it, and various other alpha and _x / _y tweens which are manipulated by this actionscript. So would be really useful if i can include the code on the buttons without having to import the mx transitions each time.
I guess i have two options:

1) Include all the code on main timeline frame one, with each button instance named (no need to put code on the button this way).
2) Use different tweening code with easing - is this possible? Anyone know where i can copy some from? :p

Thanks again

Malc
12-08-2007, 07:31 PM
Just pop all your code on the main timeline - that's how it should be used.

The real way is to use classes and get all the code away from the application timeline altogether so that it is imported upon compilation. But, for what you are doing here, placing it on the main timeline is fine. Don't bother recreating the wheel; the code for tweening is there for you to use and if you start rebuilding it, the chances are, it will probably end up less functional, heavier in file size and will take up your time.

Just get those button controls on the main timeline and import the classes once only.

Beware that when applying code to the button event methods, e.g. onRelease() { ... }, it will be executed within the scope of the button, so all reference must be targetted back to the root of the application.

Cheers,


Malc.