A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: refrencing in AS3

  1. #1
    Member
    Join Date
    Nov 2005
    Posts
    75

    Question refrencing in AS3

    im trying to make a grid in flash where i got a square in the librray classed as Unit. i also have the class Grid with this in it:

    Code:
    package{
    	import flash.display.*;
    	import flash.events.*;
    	public class Grid extends MovieClip {
    		private static const rows:int = 16;
    		private static const cols:int = 16;
    		public function Grid(){
    			for (var i:int=1; i<=cols; i++){
    				for (var j:int=1; j<=rows; j++){
    					var unit:Unit = new Unit();
    					unit.name = "U"+i+"_"+j;
    					unit.c = i;
    					unit.r = j;
    					unit.x = i*28;
    					unit.y = j*28;
    					addChild(unit)
    				}
    			}
    		} 
    	}
    }
    and this as the document class:

    Code:
    package{
    	import flash.display.*;
    	import flash.events.*
    	public class Main extends MovieClip {
    		public var grid:Grid = new Grid();
    		public function Main(){
    			addChild(grid);
    		}
    	}
    }
    in AS2 when you attached a movieclip to wherver, you could trace it and identify where it was and it existed. but now i cannot refrence an individual block on my grid outside of the Grid function let alone the grid class. where is it gone and where does "addChild" put the instance?
    Last edited by jazz090; 06-27-2008 at 10:46 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    addChild puts the instance on the displayList and nowhere else. You could get them using getChildByName("U1_1"), etc, but that's not a very good way to do it.

    Instead, I'd suggest creating a two dimensional array of Units and store them there.

    Code:
    package{
    	import flash.display.*;
    	import flash.events.*;
    	public class Grid extends MovieClip {
    		private static const rows:int = 16;
    		private static const cols:int = 16;
                    private var units:Array; //will be 2d array of Unit
    		public function Grid(){
                            units = new Array(); //initialize cols
    			for (var i:int=1; i<=cols; i++){
                                    units[i] = new Array(); //initialize rows
    				for (var j:int=1; j<=rows; j++){
    					var unit:Unit = new Unit();
    					unit.name = "U"+i+"_"+j;
    					unit.c = i;
    					unit.r = j;
    					unit.x = i*28;
    					unit.y = j*28;
    					addChild(unit);
                                            units[i][j] = unit; //store unit for later use
    				}
    			}
    		}
    
               public function getUnit(col:int, row:int):Unit{
                  return units[col][row];
               } 
    
               //careful, this won't remove it from anywhere else in the grid
               public function setUnit(col:int, row:int, u:Unit):void{
                  units[col][row] = u;
               }
    	}
    }

  3. #3
    Member
    Join Date
    Nov 2005
    Posts
    75
    thank you man, great help. but one problem, if i make the array wont i make refrences to the objects of the array instead of the ones in the display list?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes and no:

    Yes, you are referencing the objects in the array. No, you're not referencing them "instead" of the ones in the display list, because they're the same objects. That's the whole point of referencing instead of copying.

  5. #5
    Junior Member
    Join Date
    Apr 2008
    Location
    Troy, MI
    Posts
    22
    nope, the array does not create a copy of the objects, like it would a String or Number, it in fact just makes a variable that references your object (commonly known as a pointer) When you call the item in the array, it will point directly to the item it was created to point to.

    Also, I'd like to explain addChild() a little better. It does add an object to the display list, but what I believe you were asking is where does it add it. addChild is not a Top-Level function, it is a method of any object that can contain other objects, like a MovieClip, so when you call addChild, you are actually calling this.addChild(), this being the object you are writing the code in. Just the same, you can call myClip_mc.addChild() and it will add the child inside that movie clip.

  6. #6
    Member
    Join Date
    Nov 2005
    Posts
    75
    thank you for all the help, you people saved my day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center