Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can add an event listener to the stage and use <code>Event.ADDED</code> to get a reference to any display object added anywhere in the display list. </p> <p>Then simply type cast if the added item is a subclass of <code>ManagedSprite</code> (BTW, the convention is to start your class names with an uppercase letter):</p> <pre><code>stage.addEventListener (Event.ADDED, onAdded); function onAdded( ev:Event ):void { if (ev.target is ManagedSprite) { var managed:ManagedSprite = ManagedSprite( ev.target ); doStuffWith( managed ); } } </code></pre> <p><strong>EDIT</strong> </p> <p>I think I only just understood your question: You are not trying to do an actual type cast - which would require your class hierarchy to be already set up, i.e. you'd have to have extended the ManagedSprite class already - but to add functionality at runtime!</p> <p>I would strongly discourage you from trying to do deep copies or such - it will be heavy on performance, depending on how many sprites you are going to add, and you will no longer have your compiler to help you prevent errors. </p> <p>Rather, see if you can't favor composition over inheritance: Create a kind of "proxy" class for the sprite, let's call it <code>ManagedSpriteProxy</code>, which implements all the methods you would call on ManagedSprite, but forwards all the actual manipulations to its `managedSprite' property. Then use the event handler I outlined above to create the proxy objects and attach the respective sprites:</p> <pre><code>public class ManagedSpriteProxy { private var _managedSprite:Sprite; public function ManagedSpriteProxy( managedSprite:Sprite ) { this.managedSprite = managedSprite; } public function get managedSprite():Sprite { return _managedSprite; } public function set managedSprite( managedSprite : Sprite ):void { _managedSprite = managedSprite; setUpAnyHandlersOrWhatever(); } private function setUpAnyHandlersOrWhatever():void { // Many wonderful things happening } // Many more wonderful things happening via public API } // somewhere else public class SpriteManager { private var _managedSprites:Array = []; public function SpriteManager() { addEventListener( Event.ADDED_TO_STAGE, onAddedToStage ); } private function onAddedToStage( ev:Event ):void { removeEventListener( Event.ADDED_TO_STAGE ); stage.addEventListener( Event.ADDED, onAdded ); } private function onAdded( ev:Event ):void { if( ev.target is Sprite ) { addWithProxy( ev.target as Sprite ); } } private function addWithProxy( sprite:Sprite ) : void { var proxy:ManagedSpriteProxy = new ManagedSpriteProxy( sprite ); _managedSprites.push( proxy ); } // Insert here whatever methods used to manage the sprites, // all of them call the proxies instead of the sprites! } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload