Contact Yart for a relaxed conversation
Your Name * Message *
Your Email *  
Your Phone   
Our Phone:        03 8685 8718 (within Australia)
61 3 8685 8718 (outside Australia)
Contact Yart

Melbourne Content Management and Flash developers

 

for designers, agencies and your website

Resources / Flash / Looping through controls
Looping through controls in Flash

In this example we are going to assign a single click handler to several objects. The click handler is to create an instance of a symbol, which name is the same as the object being clicked.

This is how:

  1. Convert the objects to Button or Movie Clips so they can be named
  2. Assign names to the every object (eg. Circle, Square, etc), which correspond to the Symbol name you want to load when it is clicked. This means you will have symbols called Circle, Square, etc.
  3. Group these objects into a Symbol, and give it a name, eg. Palette.
  4. This is the code to loop through all objects in Palette and assign the click handler:
    for(var i=0; i < Palette.numChildren; i++) {
    Palette.getChildAt(i).addEventListener(MouseEvent.CLICK, handleClick);
    }
  5. This is the handler, which loads a symbol with the same name as the clicked object:
    function handleClick(e:MouseEvent) {
    //trace(e.target.name);
    var LargeShape:Class = getDefinitionByName(e.target.name) as Class;
    var myShape = new LargeShape();
    addChild(myShape);
    }
The latest at Yart
Search: