0

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?

asked May 25, 2010 at 14:29

2 Answers 2

1

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

answered May 25, 2010 at 14:34
Sign up to request clarification or add additional context in comments.

6 Comments

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?
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...
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: 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 MovieClips, if that helps.
I'll post it in another question, seeing as how it's different than the original. Thanks for everything.
|
0

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.

answered May 25, 2010 at 14:45

2 Comments

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.