views:

56

answers:

2

Hi there, this is my first time posting a question here.

I have an array of 12 buttons on a timeline that when first visiting that part of the timeline, get a CLICK eventlistener added to them using a for loop. All of them work perfectly at that point.

When you click one it plays a frame label inside the specific movieClip and reveals a bio on the corresponding person with a close button and removes the CLICK eventlisteners for each button, again using a for loop. The close button plays a closing animation, and then the timeline goes back to the first frame (the one with the 12 buttons on it) and the CLICK eventlisteners are re-added, but now only the first 9 buttons of the array work. There are no output errors and the code to re-add the eventlisteners is exactly the same as the first time that works. I am completely at a loss and am wondering if anyone else has run into this problem.

All of my buttons are named correctly, there are absolutely no output errors (I've used the debug module) and I made sure the array with the buttons in it is outputting all 12 at the moment the close button is clicked to add the eventlisteners back.

    for (var q = 0; q < ackBoDBtnArray.length; q++){
        contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[q]].addEventListener(MouseEvent.CLICK, showBio);
    }

    private function showBio(eo:MouseEvent):void {
        trace("show the bio");
        bodVar = ackBoDBtnArray.getIndex(eo.target.name);
        contentArea_mc.acknowledgements_mc.BoD_mc.gotoAndPlay(ackBoDPgArray[bodVar]);
        contentArea_mc.acknowledgements_mc.BoD_mc.closeBio_btn.addEventListener(MouseEvent.CLICK, hideBio);
        for (var r = 0; r < ackBoDBtnArray.length; r++){
            contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[r]].mouseEnabled = false;
            contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[r]].removeEventListener(MouseEvent.CLICK, showBio);
        }
    }
    private function hideBio(eo:MouseEvent):void {
        trace("hide it!");
        contentArea_mc.acknowledgements_mc.BoD_mc.closeBio_btn.removeEventListener(MouseEvent.CLICK, hideBio);
        contentArea_mc.acknowledgements_mc.BoD_mc.gotoAndPlay(ackBoDClosePgArray[bodVar]);
        for (var s = 0; s < ackBoDBtnArray.length; s++){
            trace(ackBoDBtnArray[s]);
            contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[s]].mouseEnabled = true;
            contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[s]].addEventListener(MouseEvent.CLICK, showBio);
        }

Thanks in advance for any help and insight you can provide...I have a slight feeling that its something that may be obvious to another set of eyes...haha.

A: 

Something that looks suspicious to me is that you're removing the event listener from closeBio_btn at the start of the hideBio(). Try placing that line after the for loop.

I'm having trouble justifying this hunch, but a couple of wild theories that have crossed my mind are:

  1. This is a race condition. You're removing the listener for the handler from inside the handler itself, and since the handler was added dynamically, the runtime marks the handler for garbage collection. On the next sweep, the handler is deleted — but since gotoAndPlay probably takes a non-significant amount of time, the handler is still in the for loop adding click handlers when it gets killed, and has only added the first 9 at this point.
  2. This is a data-corruption problem. Since the hide button and the array of 12 buttons are at least tangentially related through BoD_mc, the runtime is doing some strange optimization behind the scenes that makes accessing items through Bod_mc[with indexes] unreliable after closeBio_btn has been touched.

This is an interesting problem, I hope you find a solution :)

Dan M
A: 

It seems to fix it I had to just add every btn dynamically instead of just having them on the timeline. Still strange though, thanks everyone

Tyler Pepper