Category Archives: AS3

MainImporter Class

This is the second custom class used in the Bullet Class, MainImporter.

During the same project that prompted TextFormatter it dawned on me I had no real useful Load this class. There are a lot of nice phat loaders around, BulkLoader is a great example. However, it’s kind of overkill for what I wanted.

So I put together MainImporter. It handles Bitmaps, SWFs and XML nicely and I’ve used it extensively for pretty much every project since.

package com.utils{
import flash.display.Loader;
public class MainImporter extends EventDispatcher{
public static const ITEMLOADED:String = “item loaded”;
private var itemURL:String;
public var loader:Loader;
public var loaderXML:URLLoader;
private var fileEnder:String;
public var loadedCurrent:Number;
public var loadedTotal:Number;
public function MainImporter(_url:String){
itemURL = _url;
var tmpArray:Array = itemURL.split(“.”);
fileEnder = tmpArray[tmpArray.length-1];
private function loadItem():void{
var urlRequest: URLRequest = new URLRequest(itemURL);
if(fileEnder == “xml”){
loaderXML = new URLLoader();
loader = new Loader();
private function initListener(evt:Event):void{
private function progressListener(evt:ProgressEvent):void{
loadedCurrent = Math.floor(evt.bytesLoaded/1024);
loadedTotal = Math.floor(evt.bytesTotal/1024);
dispatchEvent(new LoaderEvent(LoaderEvent.UPDATELOADER,loadedCurrent,loadedTotal,true));
private function loadError(evt:IOErrorEvent):void{
trace(“IOErrorEvent : “ + evt);
private function completeLoad(evt:Event):void{
dispatchEvent(new Event(MainImporter.ITEMLOADED));

This is a work in progress, so do what you want with it.

TextFormatter Class

I just posted on BulletBuilder. I realized there are two other classes I set up a while ago for another project that I use regularly that could use some explanation.

First is TextFormatter. We’d started a project and needed a quick way to implement some basic textfields across the project that could react to some supplied variables; font, color, size, leading

To circumvent some massive discussion about Stylesheets or whatever, I set up TextFormatter and it’s done the job pretty adequately and I’ve used it since for anything non-complex in a textfield.

To use TextFormatter, just import the class and create a textfield then instantiate TextFormatter. Of course, also import the TextField class. You’ll want to set the width of the textfield as well.

var tf:TextField = new TextField;
tf.width = 300;
new TextFormatter(tf,0x000000,12,“Flash ain’t so bad”,“Arial”);

Now in TextFormatter all the pseudo-heavy lifting can begin.

package com.utils {
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.AntiAliasType;
public class TextFormatter {
public function TextFormatter(src:TextField,fcolor:uint,fsize:Number,str:String,ffont:String=“FranklinGothicEF-Demi”, leading:Number=0)
src.autoSize = TextFieldAutoSize.LEFT;
src.background = false;
src.border = false;
src.embedFonts = true;
src.selectable = false;
src.antiAliasType = AntiAliasType.ADVANCED;
var format:TextFormat = new TextFormat;
format.font = ffont;
format.color = fcolor;
format.size = fsize;
format.leading = leading;
src.defaultTextFormat = format;
src.htmlText = str;

Bullet Class

A little while ago, I was working a little project that had several lists of bullets. I couldn’t find any suitable solutions that gave the designer what he craved; a list of bullets with well-wrapped text.

Sample xml;

<SAMPLE_TEXT>• Bullet item one.
• Bullet item two. Long enough to wrap because I’ve typed a little bit too much and it definitely is too long to fit in one line.
• Bullet item three. There’s nothing like seeing text wrapping to make you realize that bullets are the bane of your existence. Nothing, I say.
• Bullet item four.</SAMPLE_TEXT>

Here’s what you get when you just set up a normal textfield and do what you normally do.

var tf:TextField = new TextField;
tf.width = 300;
tf.multiline = true;
tf.wordWrap = true;
tf.text = SAMPLE_TEXT;

As you can see, the text wraps under the bullet because flash only sees it as a block of text.

I realized that the best solution, for me anyway, was to separate the bullet and the line, make two textfields and line them up. This way the copy aligns as it should and doesn’t wrap under the bullet. So I set up a little class to take care of it for me.

The XML had to change to accomodate TextFormatter, but only a little;

<SAMPLE_TEXT bulletString=“•” ffont=“Arial” fsize=“18” fcolor=“0x000000”>• Bullet item one.
• Bullet item two. Long enough to wrap because I’ve typed a little bit too much and it definitely is too long to fit in one line.
• Bullet item three. There’s nothing like seeing text wrapping to make you realize that bullets are the bane of your existence. Nothing, I say.
• Bullet item four.</SAMPLE_TEXT>

package com.display
import flash.display.Sprite;
import com.utils.TextFormatter;
import flash.text.TextField;
public class BulletBuilder extends Sprite
private var content:Sprite;
private var bulletCharacter:String;
private var bulletArr:Array;
private var contentArray:Array;
private var bullet:TextField;
private var copy:TextField;
private var bulletHeight:Number = 0;
private var fieldWidth:Number;
provide the constructor with;
the xml node
what character to look for
the width of the end field
var bulletBuilder:BulletBuilder = new BulletBuilder(sampleNode,”•”,415);
public function BulletBuilder(_copy:XML,_bulletCharacter:String,_fieldWidth:Number)
content = new Sprite;
bulletCharacter = _bulletCharacter;
fieldWidth = _fieldWidth;
bulletArr = _copy.toString().split(bulletCharacter+” “);
setBulletFields separates the copy
into an array based on the provided bullet character.
it then uses two textfields; one for the bullet character, one for the copy.
places the copy at the same y position as the bullet
and moving it to the right of the bullet
it then adds the two fields to the sprite and updates the y position for the next bullet
finally, the function adds the content sprite
private function setBulletFields(_copy:XML):void{
contentArray = []
for(var i:int = 1;i<bulletArr.length;i++){
contentArray[i] = new Sprite;
bullet = new TextField;
copy = new TextField;
copy.width = fieldWidth;
copy.multiline = true;
copy.wordWrap = true;
new TextFormatter(bullet,_copy.@fcolor,_copy.@fsize,bulletCharacter,_copy.@ffont);
new TextFormatter(copy,_copy.@fcolor,_copy.@fsize,bulletArr[i],_copy.@ffont);
copy.x = bullet.width;
contentArray[i].y = bulletHeight;
bulletHeight +=contentArray[i].height;

Here’s sample with the package(s)

Happy June

Things are busy as usual and that means not posting. That’s not to say I’m not doing anything and just to prove it…

I ran into a problem in a recent project where I had a series of buttons and I wanted to one to be active right off the bat. ie, like a gallery where the first image is the triggered from the first thumbnail.

There are two ways I found;

1. When your button isn’t actually sending any information along with it you can change your callback function to accept a null parameter.

button.addEventListener(MouseEvent.CLICK, onClick);

private function onClick(mevt:MouseEvent=null):void
//do whatever here

now I can just call the onClick function like any regular ol’ function


However, if you want to actually click the button because you need to know something that button has;

//using the same code as above

button.dispatchEvent(new MouseEvent(MouseEvent.CLICK));


Now tell me I haven’t been doing anything.

Bit-101's Three Useful Methods

Keith Peters posted on his blog last week three useful methods.

I’ve added a couple of extra methods that I’m finding I use more often than not and might as well shove into this class. Shove is an inelegant word, but I’m an inelegant guy.

//thanks, Keith.

public static function distanceCalculation(x1:Number, y1:Number, x2:Number, y2:Number):Number
var dx:Number = x1 – x2;
var dy:Number = y1 – y2;
return Math.sqrt(dx * dx + dy * dy);

//I mixed something up here. Fixed the randomRange value return. Thanks again, Keith.

public static function randomRange(start:Number, end:Number):Number
return Math.floor(start + Math.random() * (end – start));
It’s fair to note that I didn’t create these methods. They were found at various times in various locations. I just keep misplacing them and looking for them again. Good to know that it’s last time I’ll do it.

It’s nice to be spurred on to centralizing this stuff. Thanks, Keith!