views:

52

answers:

1

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.

A: 

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

Eugene
I could do more if I'll have a demoProject from your, to modify it :)
Eugene
Wow this is amazing, i didnt know you can just use the btn.addEventListener like that, been scrambling all day and you just saved me. Just have one more question, suppose now i wanted to remove items from the list like the image below.http://i221.photobucket.com/albums/dd298/ramdeen32/Computer/itemsCalculator.jpgNotice the items are there but i have no clue as to how i can say remove an item from the list once its there. I was thinking of possibly including checkboxes or something but that may be too complex. Thanks a lot!
1337holiday
Oh and here is a link to the file you requested, http://www.4shared.com/file/C9fcc9ql/priceCalculator.html
1337holiday
better use flex, man)read here for more http://blog.flexexamples.com/2010/01/27/creating-a-fancy-spark-list-control-item-renderer-in-flex-4/and try my little project i wrote for you: http://depositfiles.com/files/tqf1845sddon't forget to compile it in http://www.adobe.com/products/flashbuilder/?promoid=3tv64_6850L
Eugene
so, what will you say? ;)
Eugene
if you want I can tutor you.
Eugene
just installed flex and catalyst so im ready to do this. I looked into them as you suggested and its amazing what you can do with these programs. If you have the time then i would really appreciate you helping me a bit like you said. I have programming background in PHP oop if that helps.Thanks a lot!
1337holiday
okay, you can ask me here :) and don't forget to rate up comments ;)
Eugene
Hey Eugene, sorry i been a little busy. I tried to rate the comments but it says i have have 15 points or something like that. Anyways i wanted to ask you if you can help me finish up this thing if possible. I got most of it done its just i cant seem to get the quantities to update. I just wanna get the calculator done in flash, everything else im going to do in catalyst. Thanks a lot bro i really appreciate it. Here is the link if you need ithttp://www.4shared.com/file/VVL5dgCB/priceCalculator.html
1337holiday