29.04.2013 Views

ACTIONSCRIPT 3.0

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> <strong>3.0</strong> DEVELOPER’S GUIDE<br />

Working with strings<br />

FILENAMETITLEWHITE_THRESHHOLDBLACK_THRESHHOLD<br />

FruitBasket.jpgPear, apple, orange, and bananad810<br />

Banana.jpgA picture of a bananaC820<br />

Orange.jpgorangeFF20<br />

Apple.jpgpicture of an apple6E10<br />

The file uses a specific tab-delimited format. The first line (row) is a heading row. The remaining lines contain the<br />

following data for each bitmap to be loaded:<br />

The filename of the bitmap.<br />

The display name of the bitmap.<br />

The white-threshold and black-threshold values for the bitmaps. These are hex values above which and below<br />

which a pixel is to be considered completely white or completely black.<br />

As soon as the application starts, the AsciiArtBuilder class loads and parses the contents of the text file in order to<br />

create the “stack” of images that it will display, using the following code from the AsciiArtBuilder class’s<br />

parseImageInfo() method:<br />

var lines:Array = _imageInfoLoader.data.split("\n");<br />

var numLines:uint = lines.length;<br />

for (var i:uint = 1; i < numLines; i++)<br />

{<br />

var imageInfoRaw:String = lines[i];<br />

...<br />

if (imageInfoRaw.length > 0)<br />

{<br />

// Create a new image info record and add it to the array of image info.<br />

var imageInfo:ImageInfo = new ImageInfo();<br />

}<br />

}<br />

// Split the current line into values (separated by tab (\t)<br />

// characters) and extract the individual properties:<br />

var imageProperties:Array = imageInfoRaw.split("\t");<br />

imageInfo.fileName = imageProperties[0];<br />

imageInfo.title = normalizeTitle(imageProperties[1]);<br />

imageInfo.whiteThreshold = parseInt(imageProperties[2], 16);<br />

imageInfo.blackThreshold = parseInt(imageProperties[3], 16);<br />

result.push(imageInfo);<br />

The entire contents of the text file are contained in a single String instance, the _imageInfoLoader.data property.<br />

Using the split() method with the newline character ("\n") as a parameter, the String instance is divided into an<br />

Array (lines) whose elements are the individual lines of the text file. Next, the code uses a loop to work with each of<br />

the lines (except the first, because it contains only headers rather than actual content). Inside the loop, the split()<br />

method is used once again to divide the contents of the single line into a set of values (the Array object named<br />

imageProperties). The parameter used with the split() method in this case is the tab ("\t") character, because the<br />

values in each line are delineated by tab characters.<br />

Last updated 4/22/2013<br />

21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!