Return to Snippet

Revision: 2454
at February 22, 2007 00:28 by garamond


Initial Code
// EventTest.as

class EventTest extends MovieClip {
	var countEvent:MovieClip;
	
	public function EventTest() {
		countEvent = this.attachMovie(
			"countEvent",
			"countEvent",
			this.getNextHighestDepth()
		);
		countEvent.addEventListener("countComplete", this.countComplete);
		countEvent.startCount();
	}
	
	public function countComplete(event:Object):Void {
		trace("Count Complete");
	}
}

//----------------- separated files -----------------

// CountEvent.as

import mx.events.EventDispatcher;

dynamic class CountEvent extends MovieClip {
	var counter:Number;
	var max:Number;
	
	public function CountEvent() {
		EventDispatcher.initialize(this);
		counter = 0;
		max = 50;
	}
	
	public function startCount():Void {
		this.onEnterFrame = function():Void {
			counter++;
			trace(counter);
			if(counter >= max) {
				delete this.onEnterFrame;
				this.dispatchEvent({type: "countComplete"});
			}
		}
	}
}

Initial URL

                                

Initial Description
A CountEvent class’s instance counts up from 0 to 50, then dispatches "countComplete" event. Here’s a result below.

1
2
3
.
.
.
48
49
50
Count Complete

Initial Title
Study for EventDispatcher

Initial Tags
class, event

Initial Language
ActionScript