Strange ActionScript dispatchEvent / addEventListener Gotcha

I came across a strange situation over the weekend where a custom event being broadcast through dispatchEvent() was not being caught by the registered listener object. To make things even more bizarre, hasEventListener() definitely showed the event as registered, and I could even trace the type out through willTrigger().

Relevant example code is below:

	private function fileDoesNotExist(e:IOErrorEvent):void {
		textSlide = new TextSlide(data.comment);
		textSlide.addEventListener("slideReady", writeIt);
	}
	
	private function writeIt(e:Event):void {
		// Okay!
	}

After a bunch of experimentation and comparing this structure with similar classes, I found that simply delaying the event dispatcher with a Timer by a few milliseconds resulted in a successful dispatch and capture.

Now, the class transmitting the event is a very simple one that simply has a constructor function that renders some bitmapData from injected data (severely shortened for this example):

package edu.du.image {
	import edu.du.image.ResizeImage;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	[Event(name="slideReady", type="flash.events.Event")]

	public class TextSlide extends Bitmap {
		
		public function TextSlide(t:String, bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=true){
			super(bitmapData, pixelSnapping, smoothing);
			
			/*
			
			perform a set of processes to render text to bitmapData
			almost all is removed in this example
			you get the idea...
			
			*/
			
			var timer:Timer = new Timer(500, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, eGo);
			timer.start();
		}
		
		private function eGo(e:TimerEvent):void {
			dispatchEvent(new Event("slideReady"));
		}
	}
}

Putting the Timer delay on there fixes everything. The only thing I can think of is that the constructor function must execute so quickly that the listener on the parent just hasn’t completely registered yet!

One of those things that can drive you nuts and waste hours of time.

5 thoughts on “Strange ActionScript dispatchEvent / addEventListener Gotcha”

  1. Just out of curiosity, could the issue with that listener stem from the fact that you are adding the event listener after the event gets fired?

    If you notice, you are instantiating the TextSlide class and then adding the listener. The event that you are listening for is being fired in the constructor which executes before your listener is created. That is why the timer works for you.

  2. Yep. Really tripped me up for awhile though! I try to document things like this as a reminder to myself and to save others time.

    Sometimes the obvious things are what end up costing you the most.

  3. Hi Joseph, thanx for the solution…spent hours trying to understand what the #### could be the problem….SOLVED!!!

  4. I just experienced the exact same problem.
    I did create an event listener for the event before I dispatched it but it was not caught.
    When I delayed the dispatching for 10 ms with a Timer, it worked.

    It appears to take a while before an instance of my class is able to dispatch events. If anyone has a more concrete explanation, great.

Leave a Comment

Your email address will not be published. Required fields are marked *