Skip to main content
Inspiring
July 7, 2011
Answered

adding custom properties to an object. AND does an object have to be declared

  • July 7, 2011
  • 2 replies
  • 817 views

Hi

I have seen the following code

var mc:MovieClip = new MovieClip;

     mc.id = 1;

Which means you can just invent whatever custom property you want attach it to the object. Is this true?

like

mc.colour = "red"

mc.speed = 20

etc...

Also - I have seen an object that wasn't even declared

eg:

mcContainer = addChild(game1)

Well that mcContainer isn't even declared so how on earth can it exist??? You have to instantiate a class to get an object don't you?

This topic has been closed for replies.
Correct answer Ned Murphy

You can assign properties to a MovieClip dynamically.  Howvever this does not apply to all objects.  For instance, you cannot do this for Sprites and SimpleButtons.

You should declare all variables/objects.  If you are operating in strict AS3 mode, that line alone would throw  errors for two reasons... neither game1 nor mcContainer is declared.  When you are not working in strict mode, you can get away with sloppy coding.  Try the following in a file that is set up for strict mode and see what you get...

var game1:MovieClip = new MovieClip();
mcContainer = addChild(game1);

2 replies

Inspiring
July 7, 2011

In addition to Ned's posts, MovieClip is a dynamic class - its documentation states it.

If class is dynamic - one can create dynamic properties and functions on it at runtime. You can create dynamic classes yourself by stating:

public dynamic class MyClass....

Inspiring
July 7, 2011

thank you.

That was very helpful too - I didn't know - there seem to be quite a few basic things I'm not aware of but hey I'm coming across them and learning all the time

Ned Murphy
Ned MurphyCorrect answer
Legend
July 7, 2011

You can assign properties to a MovieClip dynamically.  Howvever this does not apply to all objects.  For instance, you cannot do this for Sprites and SimpleButtons.

You should declare all variables/objects.  If you are operating in strict AS3 mode, that line alone would throw  errors for two reasons... neither game1 nor mcContainer is declared.  When you are not working in strict mode, you can get away with sloppy coding.  Try the following in a file that is set up for strict mode and see what you get...

var game1:MovieClip = new MovieClip();
mcContainer = addChild(game1);

Inspiring
July 7, 2011

Thanks

Ned Murphy
Legend
July 7, 2011

You're welcome