Category Archives: sample

Particle by GalaxyGoo

Near the end of 2009 I was asked to participate in the GalaxyGoo flash-a-thon themed “Particle”. I had just begun experimenting with an Orbit class and Hype and felt like I had a little something to contribute.

Kristin Henry, the founder of GalaxyGoo, brought several copies of the printed book to Flashbelt. I am so stoked to see my work in print!

Thanks again to Kristin for the opportunity! The book can be purchased here

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 com.events.LoaderEvent;
import flash.display.Loader;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
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];
loadItem();
}
private function loadItem():void{
var urlRequest: URLRequest = new URLRequest(itemURL);
if(fileEnder == “xml”){
loaderXML = new URLLoader();
loaderXML.addEventListener(IOErrorEvent.IO_ERROR,loadError);
loaderXML.addEventListener(ProgressEvent.PROGRESS,progressListener);
loaderXML.addEventListener(Event.COMPLETE,completeLoad);
loaderXML.addEventListener(Event.INIT,initListener);
loaderXML.load(urlRequest);
}else{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadError);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeLoad);
loader.contentLoaderInfo.addEventListener(Event.INIT,initListener);
loader.load(urlRequest);
}
}
private function initListener(evt:Event):void{
//trace(“init”);
}
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.
MainImporter.as

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”);
addChild(tf);

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;
}
}
}

TextFormatter.as

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;
addChild(tf);

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
sample;
var bulletBuilder:BulletBuilder = new BulletBuilder(sampleNode,”•”,415);
addChild(bulletBuilder);
=====================*/
public function BulletBuilder(_copy:XML,_bulletCharacter:String,_fieldWidth:Number)
{
content = new Sprite;
bulletCharacter = _bulletCharacter;
fieldWidth = _fieldWidth;
bulletArr = _copy.toString().split(bulletCharacter+” “);
setBulletFields(_copy);
}
/*=====================
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].addChild(bullet);
contentArray[i].addChild(copy);
contentArray[i].y = bulletHeight;
bulletHeight +=contentArray[i].height;
content.addChild(contentArray[i]);
}
addChild(content);
}
}
}

Here’s sample with the package(s)