Search code examples
actionscript-3flash-cs4parent-child

Actionscript Child within parent within parent


I am trying to make a calculator where a user can select from a list of items. If a user clicks say "ITEM1", it should add the item to a "CONTAINER_MC". The problem i have is all my data is set inside an array containing names and prices like the code below.


var menuNames:Array = [
 "Item1",
 "Item2",
 "Item3",
 "Item4",
 "item5",
 "item6"
];

//price array
var menuPrices:Array = [
 "0.99",
 "1.99",
 "2.99",
 "5.99",
 "6.99",
 "10.99"
];

Now i have a sprite which creates a menu for each of these items by the use of a movie clip containing 2 input fields which i setup like the code below.

var menuSprite:Sprite = new Sprite(); 

var totalItems:Number = menuNames.length;
var item:menuItem; //new item field
var btn:add_btn;
for(var i = 0; i < totalItems; i++) {
 item = new menuItem(); 
 btn = new add_btn();
 menuSprite.addChild(item);
 item.addChild(btn);
 item.x = 0;
 item.y = i * 80;
 btn.y = 45;
 item.itemName.text = menuNames[i];
 item.itemPrice.text = "$" + menuPrices[i];  
}
addChild(menuSprite);

This all works fine so far, the problem is that i have a button inside my item and i need to add even listeners for these buttons, the problem is how to target these buttons. Since these buttons are added through the for loop they are not given instance names so notice how i targetted the input fields stored within the "item", i used itemName but how would i do it to the buttons stored inside item.

Thank you, really appreciate any help possible.


Solution

  • Something like this:

    var menuSprite:Sprite = new Sprite(); 
    
            var totalItems:Number = menuNames.length;
            var item:menuItem; //new item field
            var btn:add_btn;
            for(var i = 0; i < totalItems; i++) {
                item = new menuItem(); 
                btn = new add_btn();
                btn.addEventListener(MouseEvent.CLICK,clickHndlr);
                menuSprite.addChild(item);
                item.addChild(btn);
                item.x = 0;
                item.y = i * 80;
                btn.y = 45;
                item.itemName.text = menuNames[i];
                item.itemPrice.text = "$" + menuPrices[i];  
            }
            addChild(menuSprite);
    
            public function clickHndlr(event:MouseEvent):void
            {
                ((event.currentTarget as add_btn).parent as menuItem).itemName.text="Any of changes";
            }
    

    But also I'd like to change arrays to Dictionary instance, like this:

    var menuData=new Dictionary();
    
            //filling our Dictionary instead of weird arrays
            for(var i:int=0;i<10;i++)
                menuData["Item"+i]=Math.round(Math.random()*100);
    
            var menuSprite:Sprite = new Sprite(); 
    
            var item:menuItem; //new item field
            var btn:add_btn;
    
            for(var menuName:Object in menuData)
            {   
                item = new menuItem(); 
                btn = new add_btn();
                btn.addEventListener(MouseEvent.CLICK,clickHndlr);
                menuSprite.addChild(item);
                item.addChild(btn);
                item.x = 0;
                item.y = i * 80;
                btn.y = 45;
    
                var menuPrice:int=menuData[menuName] as Number;
    
                item.itemName.text = menuName as String;
                item.itemPrice.text = "$" + menuPrice.toString();  
            }
            addChild(menuSprite);
    
            public function clickHndlr(event:MouseEvent):void
            {
                ((event.currentTarget as add_btn).parent as menuItem).itemName.text="Any of changes";
            }
    

    And if to be very honest, I'll put away Flash, and prefer yo use Flex+Catalyst to create great code with great UI.

    Ask more if you need more information.

    Regards Eugene