actionscript-3flashairaddchilddisplaylist

How do I pass reference of an initialised object from one class to another class, to add (addChild) to display list?


I created this simple example because I was using a more complex class, a menu item that I wanted to initialise all the settings in the Main class and then add it in in the Game class (and updating it) when needed (both classes are separate)

Class: Main (document class, is (ideally) where everything is initialised/created)

    public class Main extends MovieClip
    {
            //testing passing on reference to Game
        private var testBitmap:Bitmap;
        private var testBitmapData:BitmapData;
        private var testArray:Array;

            public function Main():void
            {
                testBitmapData = new BitmapData(256, 256, false, 0xCCDDEE);         
                testBitmap = new Bitmap(testBitmapData);            
                testArray = [];
                testArray.push(testBitmap);    //Array for reference

                game = new Game(540, 960, testArray); 
                    //create Game class, pass reference to Object
                game.x = 0;
                game.y = 0;
            }
     }

Class: Game (created by document class, is (ideally) where everything runs)

    public class Game extends MovieClip
    {

            private var testingArray:Array

            public function Game(gameWidth:int, gameHeight:int, testArray:Array):void
            {
                    this.testingArray = testArray;  //assign to local Array and access
                    addChild(testingArray[0]);  
             //addChild to Game from an item intialised in Main, doesn't work >___<
            }
    }

.

.

.

the thing is, in my original Game class; it receives an initial bundle of cached BitmapData and a list Array that tells it which BitmapData it needs to cycle through cut-down here (and that reference only for updating works (if I addedChild in Main already):

    public function Game(gameWidth:int, gameHeight:int, cachedBitmapClipArray:Array)
    {

            this.cachedBitmapClipArray = cachedBitmapClipArray;


            private function update():void
            {           
                for each (tempCachedBitmapClip in cachedBitmapClipArray)  
                {
                    tempCachedBitmapClip.updateCurrentTile();   
          //but updating through the reference to an item initialised in Main works !!
                }
            }

    }

.

how do I make the reference and passed in objects (or have access to) behave as in the original instance ?? (being able to addChild)

i.e. can objects cross 'scopes' (??) or should objects only be controlled (instantiated) in the class where they have been initialised


Solution

  • Well to answer the last question, yes objects can be passed from one object to another. I'm having a hard time understanding what exactly the problem is though. In generic programming terms Object or any other complex type are passed by reference, primitive types are also passed by reference but the internals of the AVM handle them in such a way as to treat them as passed by value. For a pretty clear (in my eyes at least) explanation of how arguments are passed via function/method calls, read here: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f56.html

    Check out other areas in the tree navigation on the left for more details on the language itself.

    One thing to note though I think the compiler will work around this error, in the first bit of code you posted Game has a return value of :void for a constructor there should be no declared return type since it should be implicitly typed as whatever the class is.

    To add some explanation in my own words regarding pass by reference or pass by value. Pass by value means that the value of the object, that is the bytes stored at the location pointed to by the variable are actually copied to a new memory location which is then used. What this means in practice is they are not the same bytes of memory but rather they are two separate memory locations each with the same value stored in them, modification of one value after the passing doesn't affect the original. Pass by reference is to say you pass the memory location therefore no new memory for the value is allocated, both values are actually pointing to the same memory location (the pointer to them itself may be different but that pointers both point to the same location in memory). In this case the objects are the same.

    What you're doing is advisable, dividing the labor and enapsulating particular types of functionality in classes does make the code easier to work with. I think the only problem would be if the Game object itself is never added as a child to something in the display tree (something that is attached to the stage). It appears you're creating a new Game and setting it's position but never adding it as a child to the stage, then adding anything to Game is never a part of the display tree.