A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: [HELP] dispatchEvent() event only received 4 out of 5 times?

Threaded View

  1. #1
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590

    [HELP] dispatchEvent() event only received 4 out of 5 times?

    Hey there. I'm into my 8th hour of trying to make a simple set of radio buttons in AS3, and this is starting to get embarrassing.

    http://www.birchlabs.co.uk/Invasion-5BUG.swf

    = = = = NOTE: RESOLVED! = = = =

    Okay, fixed it. The whole problem was due to my using weak references for event listeners. "Garbage collector going haywire" seems a likely enough explanation, but I've left my post below in the hope that someone could explain to me why weak-referencing my 'clicked' listener caused this crazy bug.

    UPDATE: this problem with weak references is more serious than I realized. My tickbox broke, too. I'd actually really be quite interested in why this is happening.

    ==============================

    The radio buttons in question are the playback controls in the bottom-left corner. The problem is that occasionally their parent doesn't receive the event (and at this point, the button goes inert and all further button presses will do the same). Each radio button is an instance of this button class:

    Code:
    public class RadioButtonButton extends SimpleButton {
    	public var myID:int;
    	public function RadioButtonButton(up:Bitmap, over:Bitmap, down:Bitmap, ID:int):void {
    		super(up, over, down);
    		myID = ID;
    	}
    	public override function mouseUp(event:Event):void {
    		if (state == "down") removeChild(downFrame);
    			addChild(overFrame);
    			state = "over";
    			
    			removeEventListener(MouseEvent.MOUSE_OVER, mouseOver);
    			removeEventListener(MouseEvent.MOUSE_OUT, mouseOut);
    			removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
    			removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
    			
    			dispatchEvent(new Event('clicked'));
    	}
    }
    In other words, when they receive a MOUSE_UP event, they will make themselves inert and call dispatchEvent(new Event('clicked'));. Through tracing, I can confirm that every time I click the button, it successfully detects the click and runs that block of code.

    The parent looks like this:

    Code:
    function FPSRadioButton(ngHandler:GraphicsHandler, currentChoice:int):void {
    	theButtons = [];
    	choice = currentChoice;
    	// make choice 'currentChoice' active
    			
    	if (currentChoice == 0) {
    		theButtons.push(ngHandler.getGraph("ButtonPauseLive"));
    	} else {
    		theButtons.push(new RadioButtonButton(ngHandler.getGraph("ButtonPauseInertUp"), ngHandler.getGraph("ButtonPauseInertOver"), ngHandler.getGraph("ButtonPauseInertDown"), 0));
    		theButtons[theButtons.length-1].addEventListener('clicked', beenClicked, false, 0, true);
    	}
    	if (currentChoice == 1) {
    		theButtons.push(ngHandler.getGraph("ButtonPlayLive"));
    	} else {
    		theButtons.push(new RadioButtonButton(ngHandler.getGraph("ButtonPlayInertUp"), ngHandler.getGraph("ButtonPlayInertOver"), ngHandler.getGraph("ButtonPlayInertDown"), 1));
    		theButtons[theButtons.length-1].addEventListener('clicked', beenClicked, false, 0, true);
    	}
    	if (currentChoice == 2) {
    		theButtons.push(ngHandler.getGraph("ButtonDoubleSpeedLive"));
    	} else {
    		theButtons.push(new RadioButtonButton(ngHandler.getGraph("ButtonDoubleSpeedInertUp"), ngHandler.getGraph("ButtonDoubleSpeedInertOver"), ngHandler.getGraph("ButtonDoubleSpeedInertDown"), 2));
    		theButtons[theButtons.length-1].addEventListener('clicked', beenClicked, false, 0, true);
    	}
    	if (currentChoice == 3) {
    		theButtons.push(ngHandler.getGraph("ButtonTripleSpeedLive"));
    	} else {
    		theButtons.push(new RadioButtonButton(ngHandler.getGraph("ButtonTripleSpeedInertUp"), ngHandler.getGraph("ButtonTripleSpeedInertOver"), ngHandler.getGraph("ButtonTripleSpeedInertDown"), 3));
    		theButtons[theButtons.length-1].addEventListener('clicked', beenClicked, false, 0, true);
    	}
    	
    	addChild(theButtons[0]);
    	addChild(theButtons[1]);
    	addChild(theButtons[2]);
    	addChild(theButtons[3]);
    			
    	theButtons[1].x = 15;
    	theButtons[2].x = 30;
    	theButtons[3].x = 50;
    			
    			
    	function beenClicked(e:Event):void {
    		choice = e.target.myID;
    		removeChild(theButtons[0]);
    		removeChild(theButtons[1]);
    		removeChild(theButtons[2]);
    		removeChild(theButtons[3]);
    		dispatchEvent(new Event('speedChanged')); // alert parent of same event we received.
    	}
    }
    In other words, it makes the radio buttons and adds event listeners to them, which should call beenClicked(). However, I can confirm that when a button breaks, beenClicked() does not get called (even if the event does get dispatched).

    Also relevant: upon a successful click, the whole button panel will delete and rebuild itself, * la:

    Code:
    function setSpeed(e:Event):void {
    	frameSkip = e.target.choice;
    	removeChild(radioButtons);
    	radioButtons = new FPSRadioButton(ngHandler, frameSkip);
    	radioButtons.x = 5;
    	radioButtons.y = 380;
    	addChild(radioButtons);
    	radioButtons.addEventListener('speedChanged', setSpeed, false, 0, true);
    }
    (ie, the same way it was created and initialized in the first place)

    So the question is: why? why are these events not being received, and why is it only sometimes?

    I'm really stumped on this one. Help me FK...
    Last edited by VENGEANCE MX; 02-02-2010 at 07:32 PM.
    http://www.birchlabs.co.uk/
    You know you want to.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center