Design Lab 5: Game Media   [ Monday 6pm - Rm 908]

instructor: Jonah Warren
email: jonah AT feedtank DOT com
url: http://www.playfulsystems.com/teaching/2007/gamemedia


Flash and External Data
(November 12th, 2007)
Loading Data from a Text File (variable / data pairs)
[ Download the ZIP ]

The following program has the following structure:

externalVar1=A sentence loaded from an external file.
&externalVar2=43
&externalVar3=A different sentence.

// create a new URLLoader object
var loader:URLLoader = new URLLoader();

// specify format as being variables
loader.dataFormat = URLLoaderDataFormat.VARIABLES;

// create listener to be called after loading is complete
loader.addEventListener(Event.COMPLETE, varsLoaded);

// start loading file
loader.load(new URLRequest("vars.txt"));

function varsLoaded (event:Event):void {
	// print results to textbox
    textOut.appendText("entire file: \n" + loader.data);
	textOut.appendText("\n\n");
    textOut.appendText("externalVar2: \n" + loader.data.externalVar2);
}


Loading Data from an XML File
[ Download the ZIP ]

The following program has the following structure:

<grid>
1,1,1,1,1,1,
1,1,0,1,0,1,
1,0,1,0,1,1,
1,1,0,1,0,1,
1,0,1,0,1,1,
1,1,1,1,1,1
</grid>

var loader:URLLoader;
var gridArray:Array = new Array();

var GRIDHEIGHT = 6;
var GRIDWIDTH = 6;

loader = new URLLoader();

// create listener to be called after loading is complete
loader.addEventListener(Event.COMPLETE, xmlLoaded);

// start loading file
loader.load(new URLRequest("data/gridData.xml"));

function xmlLoaded(event:Event):void {
    var myXML:XML = new XML(loader.data);
	var gridString:String = myXML.toString();
	
	// split separates comma deliminated data into an array
	gridArray = gridString.split(',');
		
	for(var i:uint=0;i<gridArray.length;i++) {	
		var xpos = i % GRIDWIDTH;
		var ypos = Math.floor(i / GRIDWIDTH);
		
		// passing xpos and ypos and initial value to GridSpot constructor
		var gridSpot:GridSpot = new GridSpot(xpos, ypos, gridArray[i]);
		addChild(gridSpot);
	}
}

package {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	public class GridSpot extends MovieClip {
	
		var _spotState:Boolean;
	
		public function set spotState(newVal:Boolean):void {
			_spotState = newVal;
			
			this.buttonMode = true;
			this.useHandCursor = true;
			
			if (_spotState) this.gotoAndStop("on");
			else this.gotoAndStop("off");
		}
	
		public function get spotState():Boolean {
			return _spotState;
		}		
		
		public function GridSpot(xpos:Number, ypos:Number, initState:uint):void {
			this.x = 37.5 + xpos*this.width;
			this.y = 37.5 + ypos*this.height;
			
			if (initState == 1) spotState = true;
			else spotState = false;
		
			this.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		public function clickHandler(evt:MouseEvent):void {
			spotState = !spotState;
		}
	}
}


Loading Data from an XML File... Then Writing To It Again
[ Download the ZIP ]

The following program has the following structure:

<grid>1,0,1,0,1,0,0,1,0,1,0,1,1,0,1,0,1,0,0,1,0,1,0,1,1,0,1,0,1,0,0,1,0,1,0,1</grid>

var loader:URLLoader;
var gridArray:Array = new Array();
var gridSpots:Array = new Array();

var GRIDHEIGHT = 6;
var GRIDWIDTH = 6;

loader = new URLLoader();

// create listener to be called after loading is complete
loader.addEventListener(Event.COMPLETE, xmlLoaded);

// start loading file
loader.load(new URLRequest("data/gridData2.xml"));

function xmlLoaded(event:Event):void {
    var myXML:XML = new XML(loader.data);
	var gridString:String = myXML.toString();
	
	gridArray = gridString.split(',');
		
	for(var i:uint=0;i<gridArray.length;i++) {	
		var xpos = i % GRIDWIDTH;
		var ypos = Math.floor(i / GRIDWIDTH);
		
		gridSpots[i] = new GridSpot(xpos, ypos, gridArray[i]);
		addChild(gridSpots[i]);
	}
}

// assigning a listener to the button object
writeToFile.addEventListener(MouseEvent.CLICK, clickHandler);

// executed on write button click
function clickHandler(evt:MouseEvent) {
	
	// writing the xml data
	var xmlData:String = "<grid>";
	
	for(var i:uint=0;i<gridSpots.length;i++) {		
		xmlData += gridSpots[i].numberState();
		if  (i < gridSpots.length-1) xmlData += ',';
	}	
	
	xmlData += "</grid>";
	
	var gameXML:XML = new XML(xmlData);
	
	// setting up direct to php file to write xml
	var urlRequest:URLRequest = new URLRequest("processXML.php");
	var form:URLVariables = new URLVariables();
	
	form.fileName = "data/gridData2.xml";
	form.fileContents = gameXML.toXMLString();
	
	urlRequest.method = URLRequestMethod.POST;
	urlRequest.data = form;
	
	// this goes to the given php page. use sendToURL if you don't want to.
	navigateToURL(urlRequest);
}

package {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	public class GridSpot extends MovieClip {
	
		var _spotState:Boolean;
	
		public function set spotState(newVal:Boolean):void {
			_spotState = newVal;
			
			this.buttonMode = true;
			this.useHandCursor = true;
			
			if (_spotState) this.gotoAndStop("on");
			else this.gotoAndStop("off");
		}
	
		public function get spotState():Boolean {
			return _spotState;
		}		
		
		public function GridSpot(xpos:Number, ypos:Number, initState:uint):void {
			this.x = 37.5 + xpos*this.width;
			this.y = 25 + ypos*this.height;
			
			if (initState == 1) spotState = true;
			else spotState = false;
		
			this.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		public function clickHandler(evt:MouseEvent):void {
			spotState = !spotState;
		}
		
		public function numberState():Number {
			if (_spotState) return 1;
			else return 0;
		}
	}
}

");
  echo("fileContents: " . $_POST["fileContents"]); 
  echo("

"); echo("Attempting to write file..."); echo("

"); $xmlString = $_POST['fileContents']; $filePath = $_POST['fileName']; if (!$file = fopen($filePath, 'w')) { echo "Cannot open file ($filename)."; exit; } if (fwrite($file, $xmlString) === FALSE) { echo "Cannot write to file ($file)"; exit; } echo "Write successful!"; fclose($file); ?>