开发者

array won't work actionscript 3

开发者 https://www.devze.com 2022-12-31 19:21 出处:网络
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,me

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);


//ri开发者_如何转开发ght 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消