loading ...
OpenClose

About

I’m a Dubai-based senior digital art director, interactive designer and creative developer with deep expertise in interactive design, front-end development, 3D and photography.

Connect

Search

REY AMIO

Digital art director & multimedia designer

loading ...

The InteractiveSprite Class

ActionScript 3 best practices dictate that when an item is no longer in use, all references to it, as well as all its event listeners, should be removed. This is done to aid garbage collection, as only unreferenced items without listeners will be disposed of when the garbage collector does a pass.

Now imagine if you have hundreds of Sprite object instances, all with listeners, on the display list, interactively or dynamically added and removed from the stage. It would be pretty tedious to remove each item’s listeners as it is removed from stage, only to reverse the process as it is added back. Wouldn’t it be nice if adding an instance automatically initializes all its interactive behaviors and removing it automatically triggers a dispose() method that automatically removes all listeners as well as runs any disposal-related code?

With this in mind, I decided to write a simple self-disposing InteractiveSprite class that includes boilerplate methods common to most interactive Sprites:

  • addListeners() – adds event listeners to the object and/or its children
  • removeListeners() – removes event listeners
  • enable() – enables mouse interactivity
  • disable() – disables mouse interactivity
  • set enabled() – boolean; triggers either enable() or disable()
  • dispose() – runs removeListeners() and any additional instance disposal code

Automation

The class relies on Event.ADDED_TO_STAGE and Event.REMOVED_FROM_STAGE to automate the init() and dispose() methods, respectively. The listeners for these events are also self-regulating, but not completely: a reset() method (automatically called on object creation) needs to be called before an InteractiveSprite that has been removed from the display list is added back, to reset the onAdded() callback.

The Code

package {
	import flash.display.Sprite;
	import flash.events.Event;
	public class InteractiveSprite extends Sprite {
		private var _enabled:Boolean;
		public function InteractiveSprite() {
			reset();
		}
		public function reset():void {
			addEventListener(Event.ADDED_TO_STAGE, onAdded, false, 0, true);
		}
		protected function onAdded(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoved, false, 0, true);
			init();
		}
		protected function onRemoved(e:Event):void {
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			dispose();
		}
		protected function init():void {
			addListeners();
		}
		protected function addListeners():void {
			//add your Event Listeners here
		}
		protected function removeListeners():void {
			//remove your Event Listeners here
		}
		public function dispose():void {
			removeListeners();
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			//add any additional disposal actions here
		}
		protected function enable():void {
			mouseChildren = true;
			mouseEnabled = true;
		}
		protected function disable():void {
			mouseChildren = false;
			mouseEnabled = false;
			removeListeners();
		}
		public function set enabled(enabled:Boolean):void {
			_enabled = enabled;
			enabled ? enable() : disable();
		}
		public function get enabled():Boolean {
			return _enabled;
		}
	}
}

Usage

Using the class is pretty straightforward. Just subclass InteractiveSprite, override the addListeners() and removeListeners() methods, and put all addEventListener() and removeEventListener() statements inside the respective overrides.

Ice and Fire

Music

My piano reduction of Ramin Djawadi’s “Ice and Fire,” the main theme music on HBO’s Game of Thrones. Performed, recorded and mixed using Garage Band on iOS.

Read more →

Singapore Chinese Garden

Photography

Singapore is a study in contrasts. Part ultra-modern and crowded, part old and sprawling. A young city by any reckoning, it nevertheless boasts pockets of local flavor, both real and artificial. One such place is the Chinese Garden, located…

Read more →

Black & White Mobile Photography

Photography

These days, when you hear the phrase “mobile photography”—especially in its iPhone-centric incarnation, “iPhoneography”—the first things that come to mind are retro filters and faux vintage processing. The results are generally tasteful, but are not infrequently reminiscent of a…

Read more →

Laguna de Bay

Photography

I recently came upon a stash of old photographs from 2007. I visited a lot of places that year, taking a lot of photos (mostly snapshots because of lack of planning), but thankfully I had the good sense to…

Read more →

Dubai Night Photography

Photography

Much has been said about Dubai’s skyline, but one thing is certain: things comes alive after dark. The desert haze and glaring sunlight mask much of the cityscape’s character, and with them gone the clustered buildings, architectural flights of…

Read more →

Hipstamatic Customization Guide

Photography

I love Hipstamatic. It takes ordinary scenes and imbues them with color, character and subtlety sorely absent from other, lesser camera applications. One can almost believe the hype wholesale—that the app is developed by artists. And, in suspension of…

Read more →

False Infrared Lightroom Presets

Photography

I’ve always been intrigued by the otherworldly character of near-infrared black & white landscapes—black skies and water, stark clouds and white vegetation. However, since I mostly do general photography, I couldn’t justify the expense of a camera conversion (which…

Read more →

The InteractiveSprite Class

Code

ActionScript 3 best practices dictate that when an item is no longer in use, all references to it, as well as all its event listeners, should be removed. This is done to aid garbage collection, as only unreferenced items…

Read more →

iPhoneography

Photography

A while back, photographer Chase Jarvis made quite a killing in the app market with Best Camera, a pioneering iPhone photography app that capitalized on a very simple premise: “the best camera is the one that’s with you.” The…

Read more →
Back To Top