views:

53

answers:

2

I have an object of type sprite. I would like to add a new field in the sprite object without deriving the class. I'm not sure is possible.

I tried to do

 object.newField = 'value';

but it's not working.

I need it because the Api expect that field in the sprite class.

A: 

Is it possible? Yes :)

But what do you mean by "it's not working"?

(I propose a question validator on SO. It should give a warning whenever a question contains "it's not working")

Bart van Heukelom
I retried and it works. I got a compilation error in flex, but it seems it was related to something else. I've made some changes so I don't know the reason for the error.
php html
+4  A: 

It is possible for objects of dynamic classes.

For example, Sprite is not a dynamic class, but MovieClip is. This will fail with "1119: Access of possibly undefined property newField through a reference with static type flash.display:Sprite":

var s:Sprite = new Sprite();
s.newField = 'value';

But this will work:

var m:MovieClip = new MovieClip();
m.newField = 'value';
Lars
Good to know. Dynamic classes are new to people coming to actionscript from other languages. I think this was the problem. I was using a sprite object(as parameter in my function) and it failed, then I switched to MC then it worked.
php html