Pass constructor variable for MC in the Library

A problem I absolutely can’t figure out.

I’m using an MVC based on the examples in the O’reilly book “AS3 Design Patterns” (the weather map). It uses Composite/Component based views.

Lets sat I have an MC in my library, called mc_graphic. Its Linkage Class is mc_graphic and Base Class is MyGraphic (bear with me here…).

In my document class I instantiate the view like this:
var _myGraphic:MyGraphic = new mc_graphic(myModel);

The MyGraphic class constructor:
public class MyGraphic(myModel:Object) {
…etc
}

This throws the following error:
1136: Incorrect number of arguments. Expected 0.

It appears to me that if a class extending MovieClip is an actual object in your library, it can not receive parameters via its constructor. Yet, many examples I’ve seen of MVC require passing the model to the constructor of a view.

My solution has been to use a setter after instantiation. Any thoughts about this though?

The syntax should be:


var _myGraphic:MyGraphic = new MyGraphic(myModel);

[quote=Anogar;2335026]The syntax should be:

ActionScript Code:
[LEFT]</p>
<p>var _myGraphic:MyGraphic = [COLOR=#000000]new[/COLOR] MyGraphicCOLOR=#000000[/COLOR];</p>
<p>
[/LEFT]

[/quote]

…in which case, my Class Linkage has to be changed from mc_graphic to MyGraphic…still get the same 1136 error regardless.

the attached works fine - are you sure the Model is instantiated before creating your graphic instance?

Try leaving the Base Class as MovieClip - that’s the standard method. If you need to use this class for multiple library items then it changes a bit, but that’s how it typically works.

You’ll note that the “linkage ID” field is just called “Class”, so it should be the name of the specific class - MyGraphic in your case. Leave the base class as MovieClip for most cases.

creatify, Anogar - you guys are awesome. Why can’t other forums be so cool as to provide such simple to understand yet organized replies?

Please check back on this thread again tomorrow. I have the next step in this “issue” I’ve been having as I dig through design patterns for the first time and will probably be back with a question. In particular, from the Component/Composite pattern as seen in the MVC chapter of the O’reilly AS3 Design Patterns book.

As a preface, to take from your Cliptest example, “Clip” has nested within it tons and tons of unique clips that all need to be of the same class, but are individually named in an organized fashion (beyond instanceXX). They are already on the stage of that clip, which is how I’d prefer it (for designer purposes). As part of the composite pattern, I believe they all too need to super the Component class’ constructor arguments, and I haven’t quite figured out how that works as well.

More reading for me - Till tomorrow then as I prep my next question…Thank you so much!! You guys are rockstars!

Happy to help! That’s an excellent book, I’m sure you’ll learn a lot. :thumb:

OK - I think if I can get an answer to this, I’ll finally be in business. See attached clip_test_setup2.zip.

The example is purposefuly left unfinished and I’ve tried to keep it as simple as possible.

What the built swf currently does:

  1. Rolling over either the blue or green squares send events to the controller.
  2. Controller tells the model to do something (I believe that the views are actually supposed to provide that data, not the controller - in my realworld app I’m just passing evt.target.name as the “custom” data).
  3. Model sends an event out, notifiying that its been updated
  4. All views receive the update notification, but custom handline by each type of view.

What I need to figure out:

  1. First, unhide the second layer in BlueClip, which has embedded within it four circles. Note that the circles absolutely must have custom-made instance names on the stage. In my realworld app, those instance names are used by the model to reference which pieces of data it needs to update.
  2. Help me implement the composite pattern. Component.as and Composite.as are included, almost verbatim from O’Reilly’s book with the event handling commented out.

The final rules I’m trying to apply:
a) The circles are graphically bound to their parent, the BlueClip. They have custom-made instance names required by the model. In my realworld app, the model is importing data that is guaranteed to directly co-relate to the circles’ movieclip instance names, so I don’t even need to hold their instance names anyplace as a referenced list (i.e. modelData[circleInstanceName].someData).
b) Either BlueClip and/or GreenClip can be added/removed at any point in time by the Document class or more likely, the Controller. When that happens, the model will have already imported a different set of data. Its OK that removing BlueClip removes all of its children.
c) The circles are each uniquely named and uniquely drawn, but are all typed of a single base class. For instance, they each might somehow send a different number to the model. Again, in my realworld app, I just need to get evt.target.name (of the child circles themselves) to the model, and that’s all the model needs to update the data.
d) GreenClip and BlueClip and all of BlueClips Circles should ALL receive “aNumberUpdated” and “bNumberUpdated” notifications from the model, each with their own handling - tho the circles would all handle in the same way.
e) Only the CIRCLES of BlueClip should handleMouseOverClip and send the event. GreenClip itself shoud also be able to send it, but BlueClip itself doesn’t need to.
f) BlueClip has no direct interaction with the user, but it would need to receive and handle events from the model in its own unique way while still passing those down to its circle children unharmed.
g) In my realworld app, I can’t have something where separate AS files are manually being made for each of the unique Circles. Imagine hundreds of these being embedded into BlueClip. I really dig that Flash creates these automatically!

As you can see, GreenClip and BlueClip are vastly different, and while the circles in BlueClip might appear different, they all behave the same way.

My confusion, for the most part, is “what” BlueClip needs to be in the Composite pattern. If its a Component, do I need to create a rootview that parents both BlueCircle and GreenCircle? Do the Circles then simply extend and override BlueClip or are they Components themselves? If the Circles are Components, how do I send them the model via constructor since they are being instantiated when the swf is made, not by any of my actionscript? Finally, how should the circles be setup in the Linkage properties?

I know that’s alot for someone to plow through, but any thoughts on moving forward with this simple example would be much appreciated. Perhaps I’m making it more complicated than it needs to be, but IMHO, this would be a very useful response to anyone who is plagued by design patterns, AS3, and “the old way” of drawing in Flash.

And if you got this far, thanks at least for reading!

OK - been fiddling with this today. One problem I’m having actually goes back to my original post.

I’ve figured out that all view instantiations inside a composite pattern are of the ‘composite’ class and extend the ‘component.’ The model (which I definitely need across the board) can then be passed into the constructor of each component and composite because they are instantiated via actionscript when the app is built - components can just super() the arguments as well.

The problem is when a component (BlueClip) embeds its own movieclips (Circles) that are already on that parent moveiclip’s stage when the swf is built. The constructor for the embedded moveiclips (Circles) will not allow arguments to be passed into them. How can I get the model to them?

I’ve tried messing around with getChildAt in BlueClip…maybe loop through all children of a class type and try to run a function that pushes the parameters to it. Can’t figure out how to ‘access’ the children’s properties tho.

Really stuck here - not sure what to do.

hmmm, there are things/ways, I believe to solve your issues.

if BlueClip has manually placed cirlceClips… you know you can access them by name within BlueClip class, and since they’re manually placed, you might want to just list them in that class. You can simply a) set up public methods or a setter on the circles so that they can receive the parameter: circleClip.setModel(yourModelPassedIntoBlueClip); b) you can set up listeners and BlueClip broadcasts to its listeners (the circles) and passes an event argument.

But, the best solution, imo, is to avoid passing the Model as a parameter into any class. You can add static props/methods to your model or set the model up as a singleton class, then access them directly from any class.

so, at the top of a class:


import com.YourModel;

//then can access via:
YourModel.staticFunctionName();

Thanks a bunch - there really is quite a trick to the what exactly you put in the Linkage panel vs. the instance names of objects on the stage. It seems that:

a) If an object’s instance name on the stage is the same as its Class name, Flash throws an error.
b) If an object’s instance name is blank, you have to loop through the parent’s children, find children of the specified type.

The problem is I generally get designs that are tons of objects on the stage, setup in a particular way, and it would suck to sit and go through them all. I might look into the javascript extensibiilty where you can loop and edit an FLA’s library.

I think I’d like to keep from importing the model. It seems less coupled that way. i.e. if I wanted to remove the objects but reinstantiate them later with a different model, I need some way to do that.

Thank ye both. Nice to get a conclusive answer. If any futher thoughts come up, please add.