The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 15 of 15

Thread: Looping though objects on the stage

  1. #1

    Looping though objects on the stage

    In AS2, if I needed to access all the movieclips on the stage, I could do this:

    for (var i in this) {
    if (typeof (this[i]) == "movieclip") {
    do stuff
    }
    }

    with the preceeding code written on the root.

    I'm having all kinds of trouble trying to do the same thing in AS3.

    The movie clips I'm attempting to access have been manually placed on the stage. They're not beind added via "addChild", if that makes a difference.

  2. #2

  3. #3
    What if I'm trying to find all instances of movieclips or buttons or any other object?

  4. #4
    Quote Originally Posted by mrbelv View Post
    What if I'm trying to find all instances of movieclips or buttons or any other object?
    As you're looping through them, use getQualifiedClassName to determine what symbol in your library it came from, and keep an object storing the number of each.

  5. #5
    Wow... this seems really complicated... for all the big talk about AS3, I have found it has taken things that were really simple and intuitive in AS2 and made them unnecessarily complicated. I wonder if the people at Adobe really understand what people are trying to do with their software.

    Thanks for your help. I think I'm going to get out of this business and start mowing lawns or something.

  6. #6
    Actually... that is completely overly complicated. There HAS to be a simpler way to do this. There has to be.

  7. #7
    Code:
    var i:int = numChildren;
    while(i--) if (getChildAt(i) is Button) trace("Button found! " + getChildAt(i));

  8. #8
    When I try that, I get an error which reads:

    "1120: Access of undefined property Button."

  9. #9
    "Button" is the classname, could be MovieClip or Sprite. I recommend reading some tutorials or books about Actionscript 3 fundamentals.

  10. #10
    for Buttons as in button components, you would need to import their definition via

    import fl.controls.Button;

    For native button symbols, it would be SimpleButton - but it all depends on what you're looking for. As Valaran said, it could be MovieClip or Sprite too. It's what you asked for in your second post.

  11. #11
    I am at such a loss with this. I am trying to replicate something in AS3 that I have done many times with AS2.

    All I want to do is loop through the buttons, then have the name of the button used to call up a specific node in the XML I already have loaded. I have done this type of thing millions of times in AS2, but trying to do this in AS3 is proving to be immenesely cumbersome.

    The following code tells me that "i" is now out of bounds. If I create a new function for the event listener, like you're supposed to in AS3, I have no way of passing the button name to that function.

    I am tearing my hair out trying to get this seemingly simple code to work.

    for( var i:int = 0; i < this.numChildren; i++ )
    {

    if (getChildAt(i) is SimpleButton)
    {

    getChildAt(i).addEventListener(MouseEvent.MOUSE_UP , function(evt:MouseEvent):void {

    trace(getChildAt(i));
    }
    )
    }
    }

  12. #12
    Your problem would still exist in AS2. The same applies with functions defined in loops that reference loop variables (see http://www.senocular.com/flash/tutor...#loopfunctions )

  13. #13
    Many people used that loop-through-all-items technique in AS2 and stored data (i) within the individual object - but that doesn't make it the proper (and by proper I mean the most expandable and most clearly understandable) way to do things. That's why AS3 doesn't come with an displayObject.findAndAssignDataToUnknownObjects() method.

    AS3 is hard to follow - I think most people have been frustrated in the beginning. One reason is because many of everyone's code snipets need to be scrapped and rebuilt ground-up in AS3. When you're not used to setting up classes in as2 and working with EventDispatcher and Listeners in as2, then these small old-ways of doing things can be extremely frustrating. Everyone feels your pain - but, I think I speak for tons of people when I say the longer you push through the initial learning curves of AS3, you'll really learn to like AS3 and many of the ways it forces you to work.

    The core concepts are always the same - many buttons that essentially do the same thing, and how to pair buttons with the appropriate data when clicked. You just have to be open to new ways of achieving the same result and do quite a bit of research to find how people are doing these very same things.

    See the attached FLA as an example, lots of comments, hope this helps.
    Attached Files Attached Files
    Last edited by creatify; October 20th, 2008 at 06:43 PM.

  14. #14
    This may help. I use Flex so you might have to change a few things. I use e4x to grab the length of the XML and the name tag.

    PHP Code:
    private var buttonCollection:XML;

    private function 
    resultHandler(event:ResultEvent):void
    {
        
    buttonCollection event.target.lastResult;
                    
        for (var 
    i:uint 0buttonCollection.item.length(); i++) 
            {
               var 
    myCoolBtn:Button = new Button();
               
    myCoolBtn.name "button" i;
               
    myCoolBtn.label bannerCollection.item.name[i];  
               
    myCoolBtn.0;
               
    myCoolBtn.y  10 * (myCoolBtn.width 60);
               
    this.addChild(myCoolBtn);
               
    myCoolBtn.addEventListener(MouseEvent.CLICKdetectButton);
            }

    Last edited by ryguy; October 20th, 2008 at 07:34 PM.

  15. #15
    OK.. so what if "detectButton" needs to have a different variable passed to it depending on which button was clicked? Say the function is going to diplay an XML node based on which button is clicked.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012