0

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

bikutaa
  • 33
  • 1
  • 7

1 Answers1

1

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.

shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • thanks for the links(!!) so I've learnt that passing parameters can be differentiated by whether it's just (behaviour) value OR reference that's sent, and so far i've been only using thinking the reference way (i.e. setting properites/variables, calling methods/functions) whereas I assumed all passed objects (especially own classes) were the actual value itself duplicated(!!) in my case the problem is'addChild(testingArray[0]);' line I assume because the testingArray only contains a reference to the Bitmap in Main, I can't actually addChild it in Game class; nothing is added to display list – bikutaa May 28 '12 at 20:46
  • Unless it's appeared somewhere else I can't see.. because addChild(testBitmap); in Main class is fine,.. But then in Game class 'testArray[0]' traces as '[object Bitmap]' (i now know this is just referencing it). Basically I want to split things in seperate classes instead of everything in one massive package of classes. (oh yeh the :void wasn't meant to be there don't know why I added it!!) – bikutaa May 28 '12 at 20:49
  • @bikutaa I edited my answer I think I see what's gone wrong possibly, thanks for trying a few things and passing back some more information. What you've confirmed is that the object is being passed through, the only way to really determine if it's the same object is to look at the memory address and/or the UID value of the object. – shaunhusain May 28 '12 at 21:32
  • thank you very very much(!!), your thorough diagnosis was spot on addChild(game) in the gameframework class was what was needed it's easy to lose track when classes start stacking up, it can be one missing line or something not in order etc, (I'm adapting a framework from Building Interactive Entertainment with Actionscript 3.0 and changing it quite a bit for the structure) It's nice to know my original intention to separate the classes will be fruitful, I've learned a lot from you how function parameters behave and what is fundemental understanding, so thanks once again^__^ – bikutaa May 28 '12 at 22:27
  • @bikutaa no problemo glad I could help. Certainly errors like this are a bit difficult to diagnose, but usually if you don't see something you expect to see (and no errors) it has to do with 1 of these: the size is not correct, the position is off screen, the items visibility isn't correct, or as in this case the object was never added to the display tree. MonsterDebugger is an awesome tool to help verify suspicions (also very easy to use). Best of luck in your endeavors. – shaunhusain May 28 '12 at 23:14
  • not quite got the hang of tracing things yet, but MonsterDebugger works with FlashDevelop(!!), have a great day – bikutaa May 29 '12 at 22:33