views:

324

answers:

4

Is it possible to pass constructor arguments to instance objects which I place on the stage? Are the instantiations of instance objects centralized somewhere as with .NET WinForms so I can just edit the xxx = new CustomRecangle() constructor?

public class CustomRectangle extends MovieClip {         
    public function CustomRectangle(width:int, height:int) {
        this.width = width;
        this.height = height;
    }
}
+1  A: 

Yes you can, but in the extended class' constructor don't forget to call the super() function witch will call the constructor of the baseclass.

Biroka
Thanks for noticing. Can you tell -how- or -where- I can specify the values to pass? 1.) I place the object on stage. 2.) ...?
Ropstah
A: 

See this answer if it s a symbol that you are attaching via the Flash IDE.

Patrick
So there is no way of providing the values for instance objects (not the default argument values) which are placed on the stage in the Flash IDE?
Ropstah
No, how to guess the width and the height when you are placing the object to the stage !? You can initialized your object after it will be added to the stage.
Patrick
i understand there is no way for the IDE to know. But there must be a place where the IDE places code to instantiate the object?
Ropstah
Why you need absolutely to do it at instanciate time ? you can initialize it later, what are the problems you want to solve.
Patrick
The IDE compiles a Flash application in a completely different way than with the Flex compiler, so you cannot really assume that everything you do on the stage is internally translated to AS code.
poke
+3  A: 

I don't believe you can do that, but you have two options:

1) You can roll it into a Flash component. Make the various properties you want to set inspectable in your AS file, compile it into a component and drop it on stage. Then you can drop multiple instances and set the parameters of those instances to be unique in each instance.

2) You can simply add an "init()" function that gets called on your object, and set it up to pass your various properties into the init() instead of the constructor.

Finally, and this I believe legally constitutes BLACK MAGIC so don't do it unless you ABSOLUTELY have to:

I had to do this once because there was literally no other way. Basically let's say you have 200 objects on stage, each with a unique instance name. It would be waaaaay too much work to go back and manually redo each of these to accept custom values, and because of the way the program is architected it's going to be really hard to write init calls on everything.

So instead, you can use this.name on each object. Write a static MyObjectManager class which holds a dictionary that holds objects mapped to instance names. Then in your constructor of your objects you can write something like "init(MyObjectManager.dict[this.name])" and bob's your uncle.

Let me know if that makes sense. Now, do understand that there are massive problems with doing it that way - it makes your code tricky to update, and it tightly couples all of your logic to your on-state naming convention so if any of your instance names are off then you're screwed.

But, it's an option. Between the three paths outlined here you should be able to achieve whatever you need!

Myk
A: 

I think your best bet is to remove the object from the Flash stage. Make it a library class. and do something like

var myRectangle = new CustomRectangle(100, 100);
myRectangle.x = 200; // These should be whatever its location needs to be
myRectangle.y = 200;
addChild(myRectangle);
ThunderChunky_SF