views:

582

answers:

2

Could someone expand and clarify the different logical instantiations of objects in actionscript? So far it seems there are 3 layers of instantiations, for lack of a better term.

The first one is declaring a variable/type.

Next is instantiating that variable with something solid in the code, like a method or function? Is this just a way to glue things together?

Then after that you instantiate it on the stage, is this something you have to do explicitly, or is it a side effect?

Is this "3 layer" concept the correct way of looking at it, kind of like the MVC for flash app logic?

A: 

I think you're confusing 'regular' objects and objects that extend DisplayObject and can be added to the stage.

You are right in assuming that you have to declare and instantiate every object.

If it is extending a DisplayObject you can then for example add it to the stage using :

stage.addChild(new ObjectName());

To apply this in a MVC environment: you should just use the DisplayObjects in the View part of your code.

Epskampie
A: 

you can instantiate objects without assigning them to a variable ... instead of storing a reference to an object in a variable, you can directly pass it to a function, as argument, or you can pass some other object as a parameter to the constructor, that'll allow the object to register it self elsewhere ... for example, this code will work perfectly:

(new URLLoader(new URLRequest("someURL"))).addEventListener(Event.COMPLETE, someEventHandler);

this code instantiates 2 objects, none of which is stored into a variable ...

so instantiation has nothing to do with variables at all, which is, why the "first step" is not a part of an instantiation process ... instantiation of an object is new SomeClass(someParam_1,...someParam_n) ...

and as Epskampie pointed out, the "third step" is also not a part of instantiation, because it only works with DisplayObjects ... it is not "instantiating on the stage", it is "putting them on the display list" ... also, most of the time, you put them into (grand)children of the stage, rather than the stage itself ... you may also simply decide not to put a DisplayObject on the display list (there are several scenarios, where that makes sense) ...

so yeah, i would not talk about "layers" ... what you describe is a possible approach of creating a DisplayObject on the display list, that consists of 3 "steps" (the first being optional), but not "layers" ... and there is no analogy to MVC ... flash DisplayObjects are the base for creating a view ... MVC in ActionScript works pretty much as in any other language ... you create model, controller and view objects as you would in any other language ... instantiation is the same for all, but for the latter, it requires an extra step, to display them ...

hope that helps ...

back2dos