StageAwareSprite

January 3rd, 2010

Here’s another handy little object. Again, it’s pretty trivial but might save you 10 minutes.

Extend StageAwareSprite instead of Sprite to get automatic handing of any stage-related activity. This simply saves you all the hassle of adding and removing the event listeners yourself.

The class has protected addedToStage, removedFromStage and stageResize methods that are triggered as you expect but which do nothing by default. They are intended to be extended so you can create your own custom behaviours.

Files:
com.aderowbotham.utils.StageAwareSprite
StageSpriteDemo.zip

Example usage – this could be the base class for a library object:

package {
	import com.aderowbotham.utils.StageAwareSprite;	

	public class Demo extends StageAwareSprite {

		public function Demo() {
			super();
		}

		override protected function addedToStage():void{
			trace("added to stage");
			trace("stageWidth = "+stageWidth,", stageHeight = "+stageHeight);
		}

		override protected function removedFromStage():void{
			trace("removed from stage");
		}

		override protected function stageResize():void{
			trace("stage resized to: "+stageWidth+", "+stageHeight);
		}
	}
}

Leap year detection

September 25th, 2009

A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.

Translated into Actionscript 3.0, that looks like this:

function getIsLeapyear(year:int):Boolean{
	var result:Boolean = false;
	var divBy4:Boolean = (year % 4 == 0);
	var divBy100:Boolean = (year % 100 == 0);
	var divBy400:Boolean =  (year % 400 == 0);

	if(divBy4){
		if(divBy100){
			if(divBy400){
				result = true;
			} else {
				result = false;
			}
		} else {
			result = true;
		}
	} else {
		result = false;
	}
	return result;
}

Remove spaces

September 24th, 2009

Busy busy busy…. I’ve been too busy to blog for longer than I can remember.

I’ve got loads of stuff to post, but it needs a bit of cleaning up first (as most of it suffered late night last-minute hacks to accommodate client changes).

But anyway, here’s a simple little String function that removes any spaces from the start or end of a String, and removes any multiple spaces from within a String.

For example:

Input: "     Hello   world        "
Output: "Hello world" 

Here’s the code (Flash AS3):

function removeSpaces(string:String):String{

	//remove spaces at start
	while(string.substr(0,1) == " "){
		string = string.substring(1);
	}
	//remove multiple spaces in middle
	while(string.indexOf("  ") != -1){
		string = string.split("  ").join(" ");
	}
	while(string.substr(-1) == " "){
		string = string.substr(0,string.length-1);
	}
	return string;
}

Might save you five minutes!

Sequence Randomiser

February 2nd, 2009

So I said I’d be back and here I am. I won’t bore you with specific excuses but I’ve honestly had a lot to do in the last few months.

Here’s another simple utility that came out of something old. It simply returns an array of consecutive integers, only in a random order, with no reptition. By default the smallest value is zero and the highest is the length of array requested – 1. However you can set the base value via an optional second argument.

A typical usage would be to trigger a number of animations in a random order.

Again, simple stuff but if it saves you 10 minutes then this post was worthwhile.

Files:
com.aderowbotham.utils.SequenceRandomiser

Usage:

import com.aderowbotham.utils.SequenceRandomiser;

//create a sequence of length 10
var mySequence:Array = SequenceRandomiser.createSequence(10);
trace(mySequence);    //e.g. 5,0,7,8,2,4,1,9,6,3

//create a sequence of length 10, from -5
mySequence = SequenceRandomiser.createSequence(10,-5);
trace(mySequence);    //e.g. -4,-5,0,-3,2,1,3,-2,4,-1

CalcTools

August 11th, 2008

Here’s a calculator utility package which contains a few handy Mathematical shortcuts.

A lot of it came out of some old game code, such as getIsInRange($p1,$p2,range) which returns true if position1 is within the specified range of position2. $p1 and $p2 are instances of PositionObject which contains coordinates.

PositionObject supports 1, 2 or 3 dimensions, but getIsInRange only works with 1 or 2-dimensional PositionObjects at the moment – i.e. z-distance is ignored. I’ll update the maths to work in 3D at some point!

Files:
com.aderowbotham.utils.calc.CalcTools
com.aderowbotham.utils.calc.CalcReturnObject
com.aderowbotham.utils.calc.PositionObject

Example usage:

import com.aderowbotham.utils.calc.CalcTools;
import com.aderowbotham.utils.calc.PositionObject;

var p1:PositionObject = new PositionObject(100,0);
var p2:PositionObject = new PositionObject(100,20);

trace("check range: "+CalcTools.getIsInRange(p1,p2,30));
//returns true because distance is 20 but the range tested was 30

There’s loads of other stuff in there too.

Source

August 10th, 2008

To kick off (and I’ve not got anything else ready) I’ve posted some AS3 source code. At the moment, this just contains my utils directory and doesn’t really do much on its own. The rest will slot into this folder structure as and when it’s ready:

code.aderowbotham.com/source

You may find a few handy bits and pieces in there. There’s not much at present but I reuse these classes a lot.