Actionscript 3 specifying events on interfaces

Posted on October 14, 2009. Filed under: Actionscript 3, Programming |

Earlier today I came across a blog regarding Actionscript and being able to specify events on interfaces. The fundamental issue was that in the current flavor of Actionscript there is no way to declare events as part of an interface contract, unlike in other object oriented languages like C#.

I gave this some more thought and attempted an implementation that would ultimately be a hybrid between the Actionscript event model and the C# even model. I attempted to make the least impact on the existing Actionscript event model all the while being able to declare events in a more staticly typed fashion.

The new approach only has one new key player in the existing Actionscript event model, namely the EventProperty. An EventProperty instance are simple objects; they take care of adding and removing event listeners for a specific event type. EventProperty’s are meant to mimic the “event” property types in C# so besides simply adding and removing event listeners, EvenProperty’s also have the ability to remove all event listeners. Everything else about the event model remains the same. I defined the EventProperty interface as follows:

public interface IEventProperty
{
	function add(listener : Function, useCapture : Boolean = false, priority : int = 0) : void
	function remove(listener : Function, useCapture : Boolean = false) : void
	function removeAll() : void;
}

This interface is for other interfaces and classes to use to declare their events. I implemented my EventProperty class like the following:

import flash.events.Event;
import flash.events.IEventDispatcher;

public final class EventProperty implements IEventProperty
{
	private var __target 	: IEventDispatcher;
	private var __type		: String;
	private var __listeners	: Array = [];

	public function EventProperty(type : String, target : IEventDispatcher)
	{
		__target = target;
		__type = type;
	}

	public function add(listener : Function, useCapture : Boolean = false, priority : int = 0) : void
	{
		var index : int = __listeners.indexOf(listener);
		if (index < 0 && (__listeners[index + 1] != useCapture))
		{
			__target.addEventListener(__type, listener, useCapture, priority);
			__listeners.push(listener, useCapture);
		}
	}

	public function remove(listener : Function, useCapture : Boolean = false) : void
	{
		__target.removeEventListener(__type, listener, useCapture);
		var index : int = __listeners.indexOf(listener);
		if (index >= 0)
		{
			__listeners.splice(index, 2);
		}
	}

	public function removeAll() : void
	{
		var listener : Function;
		var useCapture : Boolean;
		while (__listeners.length != 0)
		{
			useCapture = __listeners.pop() as Boolean;
			listener = __listeners.pop() as Function;
			__target.removeEventListener(__type, listener, useCapture);
		}
	}
}

You’ll notice that when adding events using the new event properties the argument ‘useWeakReferences’ is not used. That is to say all event listeners use strong references with this approach. An example implementation is as follows:

Box interface declaring my events.

public interface IBox
{
	function get clickEvent() : IEventProperty;
	function get rollOverEvent() : IEventProperty;
	function get rollOutEvent() : IEventProperty;
}

Box implementing the interface.

public class MyBox extends Sprite implements IBox
{
	private var __clickEvent 	: IEventProperty;
	private var __rollOverEvent : IEventProperty;
	private var __rollOutEvent 	: IEventProperty;

	public function MyBox()
	{
		__clickEvent = new EventProperty(MouseEvent.CLICK, this);
		__rollOutEvent = new EventProperty(MouseEvent.ROLL_OUT, this);
		__rollOverEvent = new EventProperty(MouseEvent.ROLL_OVER, this);
	}

	public function get clickEvent() : IEventProperty
	{
		return (__clickEvent);
	}

	public function get rollOverEvent() : IEventProperty
	{
		return (__rollOverEvent);
	}

	public function get rollOutEvent() : IEventProperty
	{
		return (__rollOutEvent);
	}
}

Read Full Post | Make a Comment ( None so far )

Recently on GDTech...

The Boost C++ Libraries on a Mac

Posted on September 28, 2008. Filed under: C++, Programming | Tags: , , , |

Actionscript 3 Hidden Gem: addFrameScript

Posted on July 28, 2008. Filed under: Actionscript 3, Programming |

AS3 Library Headaches

Posted on July 15, 2008. Filed under: Actionscript 3, Programming |

Comparing Objects in Actionscript [FINAL]

Posted on April 18, 2008. Filed under: Actionscript 3 |

AS3 Namespaces

Posted on April 18, 2008. Filed under: Actionscript 3, Link of The Day |

All Girl Arcade

Posted on April 15, 2008. Filed under: Link of The Day |

Games As Stars

Posted on April 3, 2008. Filed under: Game Design |

CartoonSmart

Posted on April 2, 2008. Filed under: Link of The Day |

Design Patterns

Posted on March 31, 2008. Filed under: Link of The Day, Programming |

Fisix Flash Physics Engine

Posted on March 31, 2008. Filed under: Actionscript 3, Engines, Link of The Day, Programming |

Liked it here?
Why not try sites on the blogroll...