Flash: Pass constructor arguments to objects placed on stage?
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:in开发者_开发知识库t) {
this.width = width;
this.height = height;
}
}
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!
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.
See this answer if it s a symbol that you are attaching via the Flash IDE.
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);
精彩评论