views:

39

answers:

3

Hello, I think the title should make it pretty clear. I was wondering how you can assign a class to an object on stage. As you would do with actionscript:

var objectname:ClassName = new ClassName();

This would make a new object, but the current object already exists, it just needs to be notified that it's of the type "ClassName" so it can inherit it's properties. I've also tried assigning the "ClassName" in the linkage as either the base path or the class name. But in either situations I get an error saying that the class needs to be unique when I use the same class on multiple objects.

So I would need something like

//example exists on stage
example.class = ClassName

Thanks

A: 

I don't think there's a way to do just do that. But I do suggest you look into the decorator design pattern The idea here is that you don't change the class, but you "decorate it" with more functions as needed.

Hope this helps !

Daniel
+2  A: 

I will answer your question with a question : why are you assigning the same class on multiple objects?

If what you want is a common behavior for those objects, you should create your class and assign it has the Base Class on those objects.

Subb
I am thinking of the wrong solution to solve this problem, I will look into another solution. Thanks :)
Martino Wullems
A: 

You seem to have this the wrong way around. You define a class in order to set specific behavior & properties for an object. In a real life example, if I want to build a radio , I will come up with a radio design & implement it. Now if I need several radios, I will use the same implementation to manufacture them.

If I now wish to turn my radio into a TV , I can't just tell my radio, hey , you're a TV now. I can either decide beforehand that I want a radio/tv object and switch behavior whenever necessary or I can create a new TV object and add the radio functionality to it by adding a radio component to my TV object.

var radio:Radio // your current object

//example 1
radio.switchToTv();

//example 2
var radioTv:Tv = new Tv( radio );
PatrickS