Tag Archives: AS3 Helper Class

AS3 Helper Class – ShowDisplayList()

We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.

Wrap()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
public class Helper
{
import flash.display.*;
/**
 * This method accepts a
 * DisplayObjectContainer instance
 * and lists the children in its
 * display list by depth, name and
 * type.
 */		
public static function ShowDisplayList
	(theDisplayObjectContainer:DisplayObjectContainer):void
{
	var len:uint = theDisplayObjectContainer.numChildren;
	for(var i:uint = 0; i<len; i++)
	{
		var theChildObject:DisplayObject =
			theDisplayObjectContainer.getChildAt(i); 
		trace("level " + i + ":" + theChildObject.name +
			" " + theChildObject);
	}
	/* output example:
		level0:spaceship [object MovieClip]
		level1:asteroid0 [object MovieClip]
		level2:asteroid1 [object MovieClip]
	*/
}
}

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var theParentClip:MovieClip = new MovieClip();
	// create a clip that will hold other clips
 
for(var i:uint = 0; i<20; i++)
{
	// create 20 MovieClips and add them to
	// theParentClip's display list
	var theChildClip:MovieClip = new MovieClip();
	theChildClip.name = "clip" + i;
	theParentClip.addChild(theChildClip);
}
 
Helper.ShowDisplayList(theParentClip);
	// display a report on what's in
	// theParentClip's display list

Comments

Whenever i teach an intro Flash/Actionscript lesson to someone, and i want to stress the importance of the trace(); command, i say that there’s usually a difference between what you think Flash is doing, and what Flash thinks Flash is doing. trace(); is a fast, easy tool to help you find a discrepency between your understanding of what’s going on, and what’s actually happening!

ShowDisplayList() helps you quickly and easily visualize what’s in your DisplayObjectContainer object and at which depth levels. It’s very handy, because looks can be deceiving – when you look at graphics on the screen, you can’t always tell when there are two or more instances of the same MovieClip sitting on top of each other, or if the clip that didn’t show up is actually sitting behind another clip, etc etc. This method sorts that all out for you, and has already saved my bacon on a few projects.

For more AS3 Helper Classses and a pile of other useful stuff, check out our Flash and Actionscript 911 feature.

AS3 Helper Class – Fence()

We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.

Fence()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Helper
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
 
	/**
	 * This method confines a Point
	 * within a Rectangle.  Useful for
	 * games where the player and
	 * enemy characters have to stay
	 * within a boundary (ie the screen!).
	 */
	public static function Fence(point:Point,
				theRect:Rectangle):Point
	{
		// Make sure the point
		// is horizontally within limits
		if (point.x &gt; theRect.right)
		{
			point.x = theRect.right;
		} else if (point.x &lt; theRect.left) {
			point.x = theRect.left;
		}
 
		// Make sure the point
		// is vertically within limits
		if (point.y &lt; theRect.top)
		{
			point.y = theRect.top;
		} else if (point.y &gt; theRect.bottom) {
			point.y = theRect.bottom;
		}
		return point;
	}
}

Usage

/* Imagine an Asteroids-style game
where you need to confine your spaceship
within the boundaries of a movieClip
instance called "spaceClip"
*/
var spaceBoundaries:Rectangle = new Rectangle(spaceClip.x,
					  spaceClip.y,
					  spaceClip.width,
					  spaceClip.height);
	// Store the outer space boundaries
var pos:Point = new Point(spaceship.x, spaceship.y);
	// Store the ship's co-ordinates
var xSpeed:Number = 30;
var ySpeed:Number = 50;
	// Set thrusters to "go!"
pos.offset(xSpeed, ySpeed);
	// add the xSpeed/ySpeed to the ship's current position
pos = Helper.Fence(pos, spaceBoundaries);
	// make sure the ship doesn't escape outer space!
spaceship.x = pos.x;
spaceship.y = pos.y;
	// position the ship on the screen

Comments

Keeping game characters and objects from leaving the play field is one of those stock-standard things you do in many, many different games – from keeping the pigs inside the sty, to keeping the ball inside the tennis court, to keeping the whatever inside the viewable screen limits. We’re sure to get a lot of mileage from the Fence() method in our future projects.

For more AS3 Helper Classses and a pile of other useful stuff, check out our Flash and Actionscript 911 feature.

AS3 Helper Class – Wrap()

We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.

Wrap()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Helper
{
	/**
	 * This method reigns in a value to keep it between
	 * the supplied minimum and maximum limits.
	 * Useful for wrapping number values 
	 * (particularly paginated interfaces).
	 * Returns the reigned-in result.
	 */
	public static function Wrap(theValue:*, 
				  minLimit:*, maxLimit:*):*
	{
		var result:* = theValue
		if (theValue < minLimit)
		{
			result = maxLimit;
		} else if (theValue > maxLimit) {
			result = minLimit;
		}
		return result;
	}
}

Usage

1
2
3
4
5
6
7
var minLimit:uint = 0;
var maxLimit:uint = 20;
 
var pageIndex:uint = 20;
 
pageIndex = Helper.Wrap(++pageIndex, minLimit, maxLimit);
// pageIndex = 0.  Hooray!  That's a Wrap!

Comments

i find myself doing this kind of thing all the time: checking to see if a number has gone out of bounds, and wrapping it back to the minimum or maximum limit i’ve set. i build many, many projects with paginated interfaces, where you click the Back or Next arrow buttons to flip the page. Clicking Next on the last page sends you to the first page, while clicking Back on the first page wraps you back around to the last page. The Wrap method helps reduce some code clutter and a bit of typing.

Of course, it’s not bulletproof. i cheated a bit by using the * wildcard for the datatypes. i did this so that the method will tolerate various numeric datatypes without complaining. Pass a string, a boolean and an array to this method all at once to see it break.

For more AS3 Helper Classses and a pile of other useful stuff, check out our Flash and Actionscript 911 feature.