A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Adding a movieclip from the stage into an array?

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    21

    Adding a movieclip from the stage into an array?

    Hello and good day to you!

    I am attempting to push an object into an array based on its instance name on the stage...how do I do this?

    On my stage I've simply made a box, turned it into a movie clip named "mcBox" and have given it an instance name of "box_mc" on the stage. Here is my current test code:

    Code:
    var energy:Number = 0.75;
    var boxArray:Array = new Array;
    
    box_mc.addEventListener(MouseEvent.CLICK, onClick);
    function onClick(e:MouseEvent):void
    {
    	if(energy >= 0.5)
    	{
    		boxArray.push(box_mc);
    	}
    	
    	trace(boxArray);
    }
    Since the default amount of energy satisfies the if statement I push the movieClip into the array and then trace it. In my output I receive "[object MovieClip]"...is there anyway to distinguish it by its instance name?

    What am I doing wrong?

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    you can assign the object a name (box_mc.name), but with AS3 objects are no longer retrieved by name. They are located by reference. Which is why you can have a loop to create multiple objects with the same "name" w/o overwriting the previous object

    Code:
    var my_array:Array = new Array();
    var my_sprite:Sprite;
    for(var i:uint=0; i<10; i++){
         my_sprite = new Sprite();
         my_sprite.name = "Sprite_"+i;
         my_sprite.graphics.beginFill(0x00000);
         my_sprite.graphics.drawRect(100,100,-50,-50);
         my_sprite.graphics.endFill();
         my_sprite.x = Math.random()*stage.stageWidth;
         my_sprite.y = Math.random()*stage.stageHeight;
         stage.addChild(my_sprite);
         my_array[my_sprite.name] = my_sprite;
    }
    each of the above "my_sprite" objects coexist, and are referenced in my_array. So if you need to do something to "Sprite_5" you can say my_array["Sprite_5"].visible = false; and so forth
    Last edited by Ralgoth; 05-06-2009 at 11:57 PM. Reason: code typo
    Search first, asked questions later.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You're actually doing everything right. Your clip is going into the array just fine. The problem is that the toString method of an Array simply calls toString on all the elements of that array, so you get the default trace output of a movieclip, which is "[object MovieClip]".

    If your clip was an instance of a particular class, and that class had a different toString implementation, you'd get that output instead.

    For your purposes, perhaps you could call this instead of trace on the array:

    Code:
    function traceArray(a:Array):void{
      var totrace:String;
      if (a != null){
        totrace = "[";
      }else{
         trace("null!");
         return;
      }
      for (var i:int = 0; i < a.length; i++){
        if (a[i] is MovieClip){
          totrace += a[i].name;
        }else{
           totrace += a[i].toString();
        }
        if (i < a.length -1){
           totrace += ", ";
        }
      }
      totrace += "]";
      trace(totrace);
    }
    But that's really going out of your way.

    Oh also, be aware that the same clip can go in the array multiple times because array contents do not have to be unique. With the code you posted, it would be in the array for each time you clicked it.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Ralgoth, I would make a minor adjustment to your code even though it should work as posted:
    Code:
    var my_array:Array = new Array();
    var my_sprite:Sprite;
    for(var i:uint=0; i<10; i++){
         my_sprite = new Sprite();
         my_sprite.name = "Sprite_"+i;
         my_sprite.graphics.beginFill(0x00000);
         my_sprite.graphics.drawRect(100,100,-50,-50);
         my_sprite.graphics.endFill();
         my_sprite.x = Math.random()*stage.stageWidth;
         my_sprite.y = Math.random()*stage.stageHeight;
         stage.addChild(my_sprite);
         my_array.push(my_sprite);
    }
    Then to reference the Sprite that would have the name "Sprite_5" (which is actually the 6th sprite), use
    Code:
    my_array[5];

  5. #5
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    I actually used push initially, but decided to change it to use the string so he can reference by name if he needs to... the loop wasn't a very good example for that though
    Search first, asked questions later.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In that example, you can access by name using getChildByName
    Code:
    stage.getChildByName("Sprite_5");

  7. #7
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    this is true. It's one of those things that I never use, and so it gets filed away into that dusty cabinet somewhere in the back of my brain.

    Ohwell, gg.
    Search first, asked questions later.

  8. #8
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    I just remembered why I used array["string"]. In a past project I had to build a 3D world for a holiday card. So to let an object enter inside a house, I had to make that object become a child of the house. Using the array helped me keep track of where all my object were.

    Though, unless your display objects jump from parent to parent, this probably isn't necessary.
    Search first, asked questions later.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't see how that follows, but that's not really relevant to the original question anyway.

    For future reference, when you want a map from Strings to objects, you can use a plain old Object, or a Dictionary. Both of those are more suited to that than an Array.

  10. #10
    Junior Member
    Join Date
    Oct 2008
    Posts
    21
    Thanks very much guys!

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