A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Dynamic Variable Reference To Movie Clips, Counting Instances?

  1. #1

    Dynamic Variable Reference To Movie Clips, Counting Instances?

    Hello,

    I'm adding movie clips (squares) using addChild. I'm also adding sub-clips (circles) to the main square clip.

    Then, to reference those movie clips, I'm using a "dynamic variable" method, for easier coding (i.e., holderObject["as1"], holderObject["as2"], etc.).

    The problem is that I'm trying to read the number of objects in holderObject. When I use holderObject.numChildren, it returns 0.

    Is there an easy way to get the number of objects in holderObject, similar to an array (holderObject.length)? Thank you.


    Code:
    // MC1 is a square-shaped object; MC2 is a circle-shaped object
    var holderObject:MovieClip = new MovieClip();
    
    // Add first main object
    var tempObject1:MC1 = new MC1();
    addChild(tempObject1);
    holderObject["as1"] = tempObject1;
    // Add child object to first main object
    var tempCircle1:MC2 = new MC2();
    holderObject["as1"].addChild(tempCircle1);
    holderObject["as1"].circle = tempCircle1;
    // Position objects
    holderObject["as1"].x = 50;
    holderObject["as1"].circle.x = -10;
    
    // Add second main object
    var tempObject2:MC1 = new MC1();
    addChild(tempObject2);
    holderObject["as2"] = tempObject2;
    // Add child object to second main object
    var tempCircle2:MC2 = new MC2();
    holderObject["as2"].addChild(tempCircle2);
    holderObject["as2"].circle = tempCircle2;
    // Position objects
    holderObject["as2"].x = 250;
    holderObject["as2"].circle.x = 50;
    
    trace (holderObject.numChildren);

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    You are not adding any children to holderObject by all accounts;
    PHP Code:
    trace(holderObject.numChildren);
    trace(holderObject["as1"].numChildren);
    trace(holderObject["as2"].numChildren); 
    PHP Code:
    // MC1 is a square-shaped object; MC2 is a circle-shaped object
    var holderObject:MovieClip = new MovieClip();

    // Add first main object
    var tempObject1:MC1 = new MC1();
    addChild(tempObject1);
    holderObject["as1"] = tempObject1;
    // Add child object to first main object
    var tempCircle1:MC2 = new MC2();
    holderObject["as1"].addChild(tempCircle1);
    holderObject["as1"].circle tempCircle1;
    // Position objects
    holderObject["as1"].50;
    holderObject["as1"].circle.= -10;

    // Add second main object
    var tempObject2:MC1 = new MC1();
    addChild(tempObject2);
    holderObject["as2"] = tempObject2;
    // Add child object to second main object
    var tempCircle2:MC2 = new MC2();
    holderObject["as2"].addChild(tempCircle2);
    holderObject["as2"].circle tempCircle2;
    // Position objects
    holderObject["as2"].250;
    holderObject["as2"].circle.50;

    holderObject.addChild(tempObject1);
    holderObject.addChild(tempObject2);

    trace(holderObject.numChildren);
    trace(holderObject["as1"].numChildren);
    trace(holderObject["as2"].numChildren); 

  3. #3
    Thanks, Fruitbeard.

    Overall, I've never understood the "array-like" notation of holderObject["xxx"] to make variable variables. It's not addressed in any of my Flash books, and I've never found a decent explanation on the web.

    All I'm trying to do is generate objects dynamically and have a way to track them, delete them, add them, etc. I'll use the method you show, with holderObject.addChild(tempObject1).

    If you know of a better way to generate objects dynamically and track them, I'd appreciate your thoughts. Much appreciated -- thank you again.
    Last edited by smcneilly; 04-30-2016 at 01:09 PM.

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