Alright, so I’ve been really hesitant to give this out… but I decided what the hell why not.
This DepthManager is a little odd in how it works… so what I will do is post it and an example, then I will answer questions.
Node.as
class Node
{
// the node that is linked to the right
public var next:Node;
// the objec this node contains
public var data:Object;
/**
* Constructor
*
* @param The object this node contains
* @param The node linked to the right
*/
public function Node( data:Object, next:Node )
{
this.data = data;
this.next = (next == undefined) ? null : next;
}
}
DepthManager.as
import Node;
/**
* A class used for managing the depths for movieclips.
* This class relies heavy on linked structures for efficiency and speed.
*
* @author Michael Avila
* @version 1.0.0
*/
class DepthManager
{
// The depth that this manager will begin allocating at
private static var startDepth:Number = 100;
// The first node in the list
private var firstNode:Node = null;
// The last node in thelist
private var lastNode:Node = null;
// Whether or not to apply the depths to the movieclips after each change
private var _autoApply:Boolean = false;
// Gets and sets the auto apply property
public function get autoApply():Boolean { return _autoApply; }
public function set autoApply( apply:Boolean )
{
_autoApply = (apply == null) ? false : apply;
}
/**
* Constructor
*/
public function DepthManager( autoApplyDepths:Boolean )
{
if (autoApplyDepths != undefined || autoApplyDepths != null) _autoApply = autoApplyDepths;
}
/**
* Returns whether this manager is empty or not
*/
public function isEmpty():Boolean
{
return (firstNode == null);
}
/**
* Find and occupy the depth before the specified clip, if the clip you specified does not exist within the depth manager
* the clip will be placed at the beginning.
*/
public function occupyBefore( beforeClip:MovieClip, clip:MovieClip )
{
if ( isEmpty() )
{
occupyFirst( clip );
return;
}
if ( beforeClip == undefined )
{
occupyFirst( clip );
return
}
var newNode:Node = new Node(clip, null);
for (var node:Node = firstNode; node != null; node = node.next)
{
if ( node.next.data == beforeClip )
{
newNode.next = node.next;
node.next = newNode;
_autoApply ? applyDepths() : return;
return;
}
}
}
/**
* Find and occupy the depth after the specified clip, if the clip you specified does not exist within the depth manager
* the clip will be placed at the end.
*/
public function occupyAfter ( afterClip:MovieClip, clip:MovieClip )
{
if ( isEmpty() )
{
occupyFirst( clip );
return;
}
if ( afterClip == undefined )
{
occupyLast( clip );
return;
}
var newNode:Node = new Node(clip, null);
for (var node:Node = firstNode; node != null; node = node.next)
{
if ( node.data == afterClip )
{
newNode.next = node.next;
node.next = newNode;
_autoApply ? applyDepths() : return;
return;
}
}
}
/**
* Occupy the first depth
*/
public function occupyFirst( clip:MovieClip )
{
var newNode:Node = new Node(clip, null);
if (isEmpty())
{
firstNode = newNode;
lastNode = newNode;
_autoApply ? applyDepths() : return;
return;
}
newNode.next = firstNode;
firstNode = newNode;
_autoApply ? applyDepths() : return;
}
/**
* Find and occupy the highest depth
*/
public function occupyLast( clip:MovieClip )
{
var newNode:Node = new Node(clip, null);
if (isEmpty())
{
firstNode = newNode;
lastNode = newNode;
_autoApply ? applyDepths() : return;
return;
}
lastNode.next = newNode;
lastNode = newNode;
_autoApply ? applyDepths() : return;
}
/**
* Removes the clip from the manager
*/
public function remove( clip:MovieClip )
{
for (var node:Node = firstNode; node != null; node = node.next)
{
if (node.next.data == clip)
{
removeNode( node );
_autoApply ? applyDepths() : return;
return;
}
}
}
/**
* Applys the model of depths, for each clip, to the actual depths of the clips
*/
public function applyDepths()
{
var count:Number = 0;
for (var node:Node = firstNode; node != null; node = node.next)
{
node.data.swapDepths( DepthManager.startDepth + count);
count++;
}
}
/**
* Pass it a node and it will remove the node after
*/
private function removeNode( node:Node ):Void
{
node.next = node.next.next;
}
/** DIAGNOSTICS **/
public function showAll()
{
for (var node:Node = firstNode; node != null; node = node.next)
{
trace( node.data );
}
}
}
fla
var dm = new DepthManager();
var box = this.createEmptyMovieClip("box", 100);
var box2 = this.createEmptyMovieClip("box2", 101);
var box3 = this.createEmptyMovieClip("box3", 102);
var box4 = this.createEmptyMovieClip("box4", 103);
var box5 = this.createEmptyMovieClip("box5", 104);
var box6 = this.createEmptyMovieClip("box6", 105);
var box7 = this.createEmptyMovieClip("box7", 106);
dm.occupyLast( box );
dm.occupyLast( box2 );
dm.occupyFirst( box3 );
dm.occupyBefore( box2, box4 );
dm.occupyAfter( box, box5 );
// Examples of non-existent befores and afters
dm.occupyBefore( boxx, box6 );
dm.occupyAfter( boxx, box7 );
dm.remove( box3 );
dm.applyDepths();
dm.showAll();
trace( box.getDepth() );
Okay, so if you have questions ask away, I have to go out for a while tonight so I will start answering if there’s any questions, when I get back.
Take Care.
_Michael