00:00
00:00
Newgrounds Background Image Theme

ChaoticGimp just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3: Objects 2012-03-17 16:42:15


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.

Response to AS3: Objects 2012-03-17 16:45:42


Objects are really useful, this tutorial will be great in AS3 Main!

Response to AS3: Objects 2012-03-19 01:13:18


I recommend anyone reading this to avoid using it if possible.


If you're a Newgrounds OG who appreciates Flash games with depth, check out the game I made in 2024.

Response to AS3: Objects 2012-03-19 04:03:41


At 2 hours ago, Jin wrote: I recommend anyone reading this to avoid using it if possible.

Type checking is your friend. If you are re-using an object like this, then you should have made it a class.

Response to AS3: Objects 2012-03-19 13:40:19


At 9 hours ago, EuPlonKa wrote:
At 2 hours ago, Jin wrote: I recommend anyone reading this to avoid using it if possible.
Type checking is your friend. If you are re-using an object like this, then you should have made it a class.

Agreed. Generic objects are nice for simple on the fly creation, but are in no way robust. The main reason being that there is no "Type" to these objects besides "Object". Plus for velocity vectors, just use the Point class.

If you want grouped variables, the two best options in my opinion are:
1. Create a class for that Object type
2. Use an array if possible