Understanding Flash Objects

I know people usually get scared when someone talks about Objects. Object always mean OOP and tt sounds very hard to understand, and all in all you can code everything you want without using objects.

That’s true, but let me clarify some concepts:

1) Using basic Flash Objects is much easier than OOP
2) You can code everything without Objects, but with Objects your life will be easier
3) Basic Objects work both in AS2 and AS3
4) Never be afraid of new techniques (print this one)
5) I will use Objects in some future tuts so it’s time to learn them

Usually Objects tutorials start with the classic phone book where you can store name, address, and so on.

You can find such examples elsewhere.

In our case, let’s suppose we have a sphere (I told I will use Objects for a game in the near future…).
The sphere has a weight, a speed and a stamina.

You can declare it in the old way

1
2
3
sphere_weight = 100;
sphere_speed = 100;
sphere_stamina = 100;

or using Objects in this way

1
2
3
4
sphere = new Object();
sphere.weight = 100;
sphere.speed = 100;
sphere.stamina = 100;

“hey”, you may say, “you used four lines to do the same thing you did with 3 lines”

That’s right, but let me show you something:

var sphere = {weight:100,speed:100,stamina:100}

in just one line… this should sound familiar to you since it’s the same declaration to initialize movieclips attributes like

_root.attachMovie("obj","obj",1,{_x:100, _y:100});

Obviously Objects aren’t intended to write more code in less lines, so I am going to jump to the point of this tutorial.

The most interesting thing about objects is you can create an array of objects. Let’s suppose we have three spheres.

You can declare them with an array of objects in this way:

1
2
3
4
var spheres = new Array();
spheres[1] = {weight:100, speed:100, stamina:100};
spheres[2] = {weight:100, speed:100, stamina:100};
spheres[3] = {weight:100, speed:100, stamina:100};

That’s all you need to know at the moment… but during next tutorials I am going to use Objects a lot, and you will see how simple can be our life with Objects.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

10 Responses to “Understanding Flash Objects”

  1. Xavi on February 6th, 2008 9:54 pm

    hmmm, I wonder how you are going to use this code….hmmmmmmmm

  2. quentin on February 6th, 2008 10:45 pm

    hi

    this tut talks about objects, but i saw on some forums that we shouldn’t use movieclips in games and every tutorials you make uses only movieclips.

    could you make a tutorial teaching how to make a game without movieclips, using bitmap?

    on the forums they were saying we should use copypixel.

    here is the link to the subject
    http://www.kirupa.com/forum/showthread.php?t=276114

  3. David on February 6th, 2008 10:46 pm

    interest tutorial

    can’t wait for the fifth part of the Metro Siberia tutorial

  4. David on February 9th, 2008 1:29 pm

    Woah, same name as me… I’d better change my name :S

    Still… This is quite interesting.

  5. Hawdon on February 10th, 2008 3:50 pm

    ………… I don’t understand O.o
    Am I stupid or something?
    What do you use objects for?

  6. brart on February 19th, 2008 10:57 pm

    What do you mean?

    “this tut talks about objects, but i saw on some forums that we shouldn’t use movieclips in games”

    You say this goes about objects(something the most people wil notice)
    than you start talking about movieclips… not very logical.

    That forum goes about as3 and as2 as standalone script. It’s better than with movieclips etc. but a lot harder it takes like 20 times as long as a normal .fla

  7. brart on February 19th, 2008 10:58 pm

    sorry for the strange thing.. it was a reply for the “quentin | Feb 6, 2008″ message

  8. Rick on February 24th, 2008 12:38 am

    Question:
    In this example

    var spheres = new Array();
    spheres[1] = {weight:100, speed:100, stamina:100};
    spheres[2] = {weight:100, speed:100, stamina:100};
    spheres[3] = {weight:100, speed:100, stamina:100};

    are each element of the array defined as an object even if new Object isnt used, because of the {name:value, name2:value} for each of them?

    how do you get the speed of sphere[2]?
    like this?:
    sphere[2].speed

  9. Tom on March 8th, 2008 11:54 am

    @Rick: Correct.

Leave a Reply




Trackbacks

  1. From zero to Bombuzal - step 3 : Emanuele Feronato - italian geek and PROgrammer on February 26th, 2008 5:41 pm

    [...] I’ll create an object to store all its information. If you have troubles with objects, refer to Understanding Flash Objects [...]

flash games company