• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Parenting and child control with AS3

Explorer ,
Mar 17, 2010 Mar 17, 2010

Copy link to clipboard

Copied

Hi,

I'm new to AS3 and although I can see some good practice and functionality I'm constantly getting stucked and can't yet make things that I would do so easily with AS2 - I almost need to relearn it from scratch!. Still I would appreciate some help and guidance on this...

I made a class that creates a "kind" of menu with data from a XML file and basically what it does when it is called is create a emptyMC (a container), add a childMC (a square) and add another child (a text label). When I create the container I'm trying to add a MouseEvent that can act on each the container instances but all I can get is a MouseEvent on object containing all the added MCs. The code follows.

Can anyone enlight me with this issue or explain what am I doing wrong.

I can't understand well the dynamic parenting with AS3 and the lack of using instance names and dot parenting doesn't help.

Thanks in advance.

Note to Adobe: Previous versions of Flash Help Files and AS reference were by far more helpful than in CS4!

In the timeline:

import assets.myDynMenu;

var theMenu:myDynMenu = new myDynMenu("menu.xml");
stage.addChild(theMenu)

stop();

In the class:

package assets

{

     import flash.display.*;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.text.TextField;
     import flash.net.*;

     public class myDynMenu extends Sprite {

         

          // INIT EXTERNAL DATA LOAD

          public function myDynMenu(theData) {

               // LOADS THE MENU DATA FROM PREFORMATTED XML
               var loader:URLLoader = new URLLoader();
               var url:URLRequest = new URLRequest(theData);
               loader.addEventListener(Event.COMPLETE, buildTheMenu);
               loader.load(url);
          }

    

          // XML MENU DATA LOAD CHECK - ON COMPLETE BUILD OBJECT AND CHILDREN

          public function buildTheMenu(event:Event):void {

               var xml:XML = new XML(event.target.data);

               for (var i=0; i<xml.menuData.menuItem.length(); i++) {

                    // ADDS A CONTAINER

                    var itemContainer:MovieClip = new MovieClip();
                    itemContainer.name = "menu" + i;
                    //

                    // THE NEXT LINE DOESN'T ACT AS I WISHED?!
                    // IN THE FUNCTION CALLED I NEED TO CONTROL THIS ITEM AND ITS CHILDREN

                    itemContainer.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);

                    //

                    itemContainer.x = 100 * i;
                    itemContainer.y = 0;
                    itemContainer.mouseEnabled = true;
                    itemContainer.mouseChildren = false;
                    addChild(itemContainer);

                    // ADDS A CHILD TO CONTAINER (a square)

                    var itemBase:MovieClip = new MovieClip();
                    itemBase.graphics.beginFill(xml.menuData.mConfig.mainItemColor);
                    itemBase.graphics.drawRect(0,0,xml.menuData.mConfig.mainItemW,xml.menuData.mConfig.mainItemH);
                    itemBase.graphics.endFill();
                    itemBase.name = "base" + i;
                    itemBase.mouseEnabled = false;
                    itemBase.mouseChildren = false;
                    itemContainer.addChild(itemBase);

                    // ADDS ANOTHER CHILD TO CONTAINER (a text label)

                    var theLabel:TextField = new TextField();
                    theLabel.x = 5;
                    theLabel.y = 5;
                    theLabel.selectable = false;
                    theLabel.mouseEnabled = false;
                    theLabel.border = true;
                    theLabel.text = xml.menuData.menuItem.mName;
                    itemContainer.addChild(theLabel);

               }
  
          }

          // THIS IS THE MOUSE OVER THAT I NEED TO ACT ON EACH itemContainer (AND CHILDREN)
          private function onMouseOver(event:MouseEvent):void {

               event.stopPropagation();
               trace("You are over " + this.name );
          }


     }

}

TOPICS
ActionScript

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 17, 2010 Mar 17, 2010

use:


package assets

{

     import flash.display.*;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.text.TextField;
     import flash.net.*;

     public class myDynMenu extends Sprite {

         

          // INIT EXTERNAL DATA LOAD

          public function myDynMenu(theData) {

               // LOADS THE MENU DATA FROM PREFORMATTED XML
               var loader:URLLoader = new URLLoader();
               var url:URLRequest = new URLRequest(theData);
               loa

...

Votes

Translate

Translate
Community Expert ,
Mar 17, 2010 Mar 17, 2010

Copy link to clipboard

Copied

use:


package assets

{

     import flash.display.*;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.text.TextField;
     import flash.net.*;

     public class myDynMenu extends Sprite {

         

          // INIT EXTERNAL DATA LOAD

          public function myDynMenu(theData) {

               // LOADS THE MENU DATA FROM PREFORMATTED XML
               var loader:URLLoader = new URLLoader();
               var url:URLRequest = new URLRequest(theData);
               loader.addEventListener(Event.COMPLETE, buildTheMenu);
               loader.load(url);
          }

    

          // XML MENU DATA LOAD CHECK - ON COMPLETE BUILD OBJECT AND CHILDREN

          public function buildTheMenu(event:Event):void {

               var xml:XML = new XML(event.target.data);

               for (var i=0; i<xml.menuData.menuItem.length(); i++) {

                    // ADDS A CONTAINER

                    var itemContainer:MovieClip = new MovieClip();
                    itemContainer.name = "menu" + i;
                    //

                    // THE NEXT LINE DOESN'T ACT AS I WISHED?!
                    // IN THE FUNCTION CALLED I NEED TO CONTROL THIS ITEM AND ITS CHILDREN

                    itemContainer.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);

                    //

                    itemContainer.x = 100 * i;
                    itemContainer.y = 0;
                    itemContainer.mouseEnabled = true;
                    itemContainer.mouseChildren = false;
                    addChild(itemContainer);

                    // ADDS A CHILD TO CONTAINER (a square)

                    var itemBase:MovieClip = new MovieClip();
                    itemBase.graphics.beginFill(xml.menuData.mConfig.mainItemColor);
                    itemBase.graphics.drawRect(0,0,xml.menuData.mConfig.mainItemW,xml.menuData.mCon fig.mainItemH);
                    itemBase.graphics.endFill();
                    itemBase.name = "base" + i;
                    itemBase.mouseEnabled = false;
                    itemBase.mouseChildren = false;
                    itemContainer.addChild(itemBase);

                    // ADDS ANOTHER CHILD TO CONTAINER (a text label)

                    var theLabel:TextField = new TextField();
                    theLabel.x = 5;
                    theLabel.y = 5;
                    theLabel.selectable = false;
                    theLabel.mouseEnabled = false;
                    theLabel.border = true;
                    theLabel.text = xml.menuData.menuItem.mName;
                    itemContainer.addChild(theLabel);

               }
  
          }

          // THIS IS THE MOUSE OVER THAT I NEED TO ACT ON EACH itemContainer (AND CHILDREN)
          private function onMouseOver(event:MouseEvent):void {

               event.stopPropagation();
               trace("You are over " + event.currentTarget.name );
          }


     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 17, 2010 Mar 17, 2010

Copy link to clipboard

Copied

Thanks, it works like a charm...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2010 Mar 17, 2010

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines