
374
|
Chapter 10, ActionScript
#85 Import ASC Files as XML
HACK
Import and Parse the ASC File
The functions handleIncoming( ) and readXMLstart( ) in the following list-
ing cause Flash to load the ASC file as an XML file and place the contents of
the file into a string,
my3dFile, if the load is successful.
We know that each line of the ASC file contains related data and ends with a
carriage return (“\n” or
Key.ENTER in Flash). To split the ASC file into sepa-
rate lines of data, we simply search through the file looking for each occur-
rence of “\n” (we don’t use String.split( ) because we want to loop through
the array anyway to look for the data of interest, as discussed later). The
readASC( ) function separates the ASC file into lines and stores them in an
array named
shape:
handleIncoming = function (success) {
if (success) {
readXMLstart( );
}
};
readXMLstart = function ( ) {
my3dFile = xObj.firstChild.nodeValue;
readASC( );
};
readASC = function ( ) {
var oIndex:Number = 0;
var rIndex:Number = 0;
var elem:String = "";
while (rIndex != -1) {
rIndex = my3dFile.indexOf("\n", oIndex);
elem = my3dFile.substring(oIndex, rIndex);
// Skip any lines that are padding (less than 30 characters)
if (elem.length > 30) {
shape[shape.length] = elem;
}
Figure 10-12. The torus shape in 3D Studio Max