views:

60

answers:

2

I've tried everything. Arrays are quite simple so I don't know why this doesn't function:

var menuList:Array = [menu_bag_mc,menu_chips_mc,menu_coke_mc,menu_cup_mc,menu_deodorant_mc,menu_fork_mc,menu_knife_mc,menu_lighter_mc,menu_milk_mc,menu_pill_mc,menu_rings_mc,menu_shampoo_mc,menu_spoon_mc,menu_straw_mc,menu_toothbrush_mc,menu_trashbag_mc,menu_water_mc];

function captureAllClicks(event:MouseEvent):void
{
    trace(menuList.indexOf(event.target));
}

stage.addEventListener(MouseEvent.CLICK, captureAllClicks);

Every time I click on any of the items on the stage (which are all given the instance names listed above. each is a tweening movieclip containing a button) I get a trace of -1. WHY?!

edit2

What needs to happen:

for each (var mc:MovieClip in menuList) mc.addEventListener(MouseEvent.CLICK, createContent);


//right here, i need to make a variable that I can put in the "addchild" so that
//for every one of the list items clicked, it adds a movieclip child with
//the same name (such as menu_bag_mc from above) with "_frame" appended.
var framevar:MovieClip = menuList[i] += "_frame";

function createContent(event:MouseEvent):void {
    if(MovieClip(root).currentFrame == 850) {
    while(MovieClip(root).numChildren > 1)
    {
        MovieClip(root).removeChild(MovieClip(root).getChildAt(MovieClip(root).numChildren - 1));
    }
MovieClip(root).addChild (framevar);
MovieClip(root).addChild (closeBtn);
}
else {
MovieClip(root).addChild (framevar);
MovieClip(root).addChild (closeBtn);
MovieClip(root).gotoAndPlay(806);
}
} 

If I can't do a variable, there's no point for the whole "for each" statement you put together... not really any point for an array, because I'll still have to create 20 lines of code for each separate one. What's the point of having an array if you can't make variables from them?

+1  A: 

because quite obviously event.target seems not to be in menuList.

the most likely explanation is, that your MovieClips have children, that are being clicked on and thus are event.target.

You should probably set mouseChildren to false on all those MovieClips. Or you could register individual handlers per movieclip as this:

function captureAllClicks(event:MouseEvent):void {
    trace(menuList.indexOf(event.currentTarget));
}
for each (var mc:MovieClip in menuList) mc.addEventListener(MouseEvent.CLICK, captureAllClicks);

greetz
back2dos

back2dos
This worked excellently. Could you explain the mouseChildren?I'm going to have a function run on every one of these list items, so do you think that would work better?
steve
I edited the above to show the new code that I have that does this function to all the items, and I'm getting the correct parameters back. Is the "for each" that you listed better? Also, is there a way to get the actual name for each list item? So that I can use it as a variable in the new function...
steve
To elaborate... say the parameter in the function is `addChild (menu_bag_mc_frame);` is there a way (such as menuList[i]) to access a variable and append "_frame"? So that on each function, it loads a new item from the library?
steve
@steve: 1. for each is slightly faster in most cases, and also provides more type safety. 2. I am not sure, I understand. Loca variable names are arbitrary and don't persist at runtime (in contrast to property names). You could simply name your `MovieClip`s, if that helps.
back2dos
I edited above to show what I need. Let me know if it's possible!
steve
I'll post it in another question, seeing as how it's different than the original. Thanks for everything.
steve
A: 

why don't you try event.currentTarget instead of event.target

Also to help yourself trouble shoot, why don't you just trace the event.target and trace the event.currentTarget. You can also loop through your array and trace all the objects in it. Then get a better visual idea of what is going on.

John Isaacks
`event.currentTarget` will allways be `stage` in this case, which is less that useless.
back2dos
@back2dos I always get e.target and e.currentTarget confused.
John Isaacks