Ok, this one’s kind of silly. I’m not entirely sure why I threw this together, really. But I got bored during lunch and here it is – a simple no-frills AS2.0 (Flash 7+) movie clip depth management class for times when you’re attaching and removing tons of clips, don’t really care what order they wind up in or what they’re called, and want to avoid running out of space.

MovieClip.getNextHighestDepth() works fine for most cases, but it’s also a wee bit dumb in that it won’t re-use depths that you skipped over. Now, this can be a desirable feature, and I might take advantage of it in an upgraded version of this class in the future… but for now, what it means is that it is very easy to programatically ratchet yourself out of available depths.

note

My version 0.1 of my SDM class (ie, the code provided here) has been tested only lightly and works fine under low stress environments. But it does seem to have issues when it starts getting hammered by lots of requests at once.

I’ve got a new version that should help remedy this problem in the works, but no schedule for when I’ll be able to release it.

the source

You can download the source here: simpledepthmanager-01.zip.


class org.simud.util.SimpleDepthManager extends MovieClip {
	///// private instance vars /////
	private var _depth:Number;
	private var _count:Number;
	private var _mcl:MovieClipLoader;

	private static var MAX_DEPTH:Number = 1048576; // 2^20

	///// public vars for listening to the mcl /////
	public var onLoadComplete:Function;
	public var onLoadError:Function;
	public var onLoadInit:Function;
	public var onLoadProgress:Function;
	public var onLoadStart:Function;

	///// constructor /////
	public function SimpleDepthManager() {
		// init our depth counter
		_depth = 1;
		// init our mc count, this is just for instance naming purposes
		_count = 0;
		// init our mcl
		_mcl = new MovieClipLoader();
		_mcl.addListener( this );
	}// end: constructor

	/**
	 * Creates and returns an empty movie clip
	 */
	public function createClip() : MovieClip {
		validateDepth();
		var mc:MovieClip = this.createEmptyMovieClip( getInstanceName(), _depth );
		return mc;
	}// end: createClip

	/**
	 * Attaches a symbol from the library and returns the new instance
	 */
	public function attachClip( id:String, init:Object ) : MovieClip {
		validateDepth();
		var mc:MovieClip = this.attachMovie( id, getInstanceName(), _depth, init );
		return mc;
	}// end: attachClip

	/**
	 * Creates an empty movie clip and loads the specified clip into it.
	 */
	public function loadClip( url:String ) : MovieClip {
		var mc:MovieClip = this.createClip();
		_mcl.loadClip( url, mc );
		return mc;
	}// end: loadClip

	private function removeClipAt( d:Number ) : Boolean {
		var mc:MovieClip = this.getInstanceAtDepth( d );
		if( mc == undefined )
			return false;
		mc.removeMovieClip();
		// and save our new target depth
		if( d = MAX_DEPTH )
			throw new Error( "Unable to find a valid new depth." );
		trace( "[debug] _depth = "+_depth );
	}// end: validateDepth
}// end: class

the explanation

So, what does it do? Well, it extends MovieClip, first off. So in order to use it, you should either extend it further yourself or simply specify the class as the code behind your container MC.

The class provides the following simple methods for dealing with clips:

createClip()
Create and return an empty movie clip.
attachClip( id:String, [init:Object] )
Attach a symbol of the specified linkage id from the library, passing it an optional init object.
loadClip( url:String )
Use an internal MovieClipLoader to load an external swf or image into a new movie clip. If you want feedback from the loading process, simply set listener functions on your instance of the manager object as if it were the MCL itself (ie, onLoadComplete, onLoadProgress, etc…).
removeClip( mc:MovieClip )
And remove a movie clip.

You never have to look at instance id’s or depth numbers when using this. I intentionally used different method names (in stead of overriding the default behaviors of the existing Movie Clip management functions) in order to leave you the freedom to use the old methods if you ever need to.

I attempt to recycle old depths once you free them – the manager will always attempt to use the lowest depth that it knows about. If, for some reason, it fails to find a free depth into which to plunk something after 1000 attempts, the manager will fall back and call getNextHighestDepth().

the license

This class is made available under the Creative Commons Attribution 3.0 License. Basically, it means that you can do with it what you will, so long as you credit me for it.