AS3: Objects
-----------------
AS3: Main
-----------------
Something I find very useful while coding is organizing my variables into objects. So what are objects? Object is a class that allows you to assign variables to it dynamically. You might think you never intented to use objects before but you used. When you assign a number to a x coordinate from a movie clip you are using an object.
The structure of an object variable is very simple. Let's say you have two velocities, one for the x coordinate and another for the y coordinate. You could just write vx and vy, or you could write:
var v:Object={x:3,y:5};
Then when you want to call one of the variables from the object, you just type the parent variable and the variable you want, like:
trace(v.x);
// or
trace(v['x']);
Which will output you 3.
If you want an object variable but with a lot of variables it might become messy but you can just set them like this:
var o:Object={};
o.a=1;
o.b=2;
o.c=3;
o.t1="hi";
o.t2="bye";
Then just call as we made before, for example:
trace(o.t1);
Will output "hi".
Another use of the object variable is creating new variables for a movie clip you added to the stage, let's say that you don't want to use alpha to set the transparency but just a number and later assign it.
movieclip.num=.5;
movieclip.alpha=movieclip.num;
So your movie clip will have 0.50 or 50% of transparency.
Another example similar to the one above is:
trace(MovieClip(root)); // outputs [object MainTimeline]
trace(MovieClip(root).x)); // outputs 0
Finally, the last example of how objects can help in organization is for the cases where you want to have two variables but with the same name in the same file.
Let's say you have two fruit shops, both have apples but with different prices. Shop A is $2.50 and shop B is $2.00. You would have trouble trying to assing price = 2.50 and price = 2.00. So you can do:
var shopA:Object={};
var shopB:Object={};
shopA.price=2.50;
shopB.price=2.00;
trace(shopA.price); // outputs 2.5
trace(shopB.price); // outputs 2
Well that is it, I hope that you enjoyed and learned something from it. Of course there are more ways of doing what I said through the tutorial but I pointed in the beginning that this is how I like to organize. You can comment about any doubt or help.
Also check out AS3: Main for more tutorials.