I’m wondering (based on scoping rules) how I might do the following:
I want to draw to a sprite that exists on the main stage in which I have a class instantiated.
So something like
public function MyClass(reference:String){
this.reference = reference;
}
public function drawToOutsideSprite(){
this.parent.getChildByName(this.reference).addChild(someLoaderName);
}
Would I use super() in this case, or what’s the usual methodology?
Thanks,
jml
There are a few ways to do this. I’m assuming your
MyClassextendsSprite.In general I would abstract out the logic for getting the “main” sprite into some util/manager class, because you don’t want to hardcode that into your MyClass, as you might need it in other places, and you might want to customize it later on. It sounds like your just asking what’s the best way to reference sprites outside of the scope of the MyClass, so I say just put it into the Util, assuming it has good reason for being their (like FlexGlobals.topLevelApplication in Flex, so you can easily access the application).
I don’t recommend passing in
id‘s orname‘s into the constructor and doing it that way, I don’t really recommend constructor arguments at all. I would just pass those into a method if you needed to, or have it built into the class itself, or the Util.To clear up the scoping question a little… You normally don’t want to draw to sprites outside the scope of the class you are in, unless they have some special functionality that will be referenced by multiple classes with totally different scopes. This is because things would start not making sense, who’s being added to who. But some good examples on when to do thatinclude:
addToolTip(child).PopUpManager.addPopUp(child), just like the sampleStageUtil.getMainSprite().addChild(child). You could even wrap that method so it’s like the one in the class above,addToStage.The
super()method isn’t useful in this scenario. The only time you really usesuper()is if you have overridden a method, and want to access the super-classes implementation. Something like this (assuming you’re extending Sprite):Otherwise, try to stick to just adding children directly to the “MyClass”.
Hope that helps.