开发者

How to draw to a sprite outside of a class

开发者 https://www.devze.com 2022-12-15 00:40 出处:网络
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.

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 MyClass extends Sprite.


package
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    /**
     *  MyClass
     */
    public class MyClass extends Sprite
    {
        public var referenceA:String;
        public var referenceB:Sprite;

        public function get referenceA_way2():Sprite
        {
            return this.parent.getChildByName(referenceA);
        }
        /**
         *  MyClass Constructor
         */
        public function MyClass(referenceA:String = null, referenceB:Sprite = null)
        {
            super();
            this.referenceA = referenceA;
            this.referenceB = referenceB;
        }

        public function drawToOutsideSpriteA(child:DisplayObject):void
        {
            // referenceA
            this.parent.getChildByName(this.referenceA).addChild(child);
            // or
            referenceA_way2.addChild(child);
        }

        public function drawToOutsideSpriteB(child:DisplayObject):void
        {
            // referenceB
            referenceB.addChild(child);
        }

        public function drawToOutsideSpriteC(referenceC:String, child:DisplayObject):void
        {
            this.parent.getChildByName(referenceC).addChild(child);
        }

        // Do this:
        // it allows you to abstract out the logic of getting the main sprite
        // into some util class, so you could reuse that functionality elsewhere,
        // and so your code is cleaner.
        public function drawToOutsideSpriteD(child:DisplayObject):void
        {
            StageUtil.getMainSprite().addChild(child);
        }
    }
}

package
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    /**
     *  MyClass
     */
    public class StageUtil
    {
        private static var root:Stage;

        /**
         *  Called when app first starts
         */
        public static function initialize(stage:Stage):void
        {
            root = stage;
        }

        public static function getMainSprite():DisplayObjectContainer
        {
            return root; // or something more complex,
            // like a recursive function to getSpriteByName
        }

        public static function addToStage(child:DisplayObject):DisplayObject
        {
            return getMainSprite().addChild(child);
        }
    }
}

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 or name'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:

  • Buttons with ToolTips: Tooltips are added to the root because they appear on top of everything, but a Button could be 20 children deep, so you'd have in the Button subclass, perhaps, addToolTip(child).
  • PopUps: You might want to add a popup from within MyClass, but it's really being added to the stage. In flex this is like PopUpManager.addPopUp(child), just like the sample StageUtil.getMainSprite().addChild(child). You could even wrap that method so it's like the one in the class above, addToStage.
  • Transform/Drawing Stage: If you have some global painting stage, or place where you scale/resize things, you might want to be able to add/remove graphics from that from any class.

The super() method isn't useful in this scenario. The only time you really use super() is if you have overridden a method, and want to access the super-classes implementation. Something like this (assuming you're extending Sprite):


override public function addChild(child:DisplayObject):DisplayObject
{
    if (child is MyDrawingSprite)
        return StageUtil.addToStage(child); // add to main stage
    else
        return super.addChild(child); // add directly to this class
}

Otherwise, try to stick to just adding children directly to the "MyClass".

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消