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

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


Loading Data from a Text File (variable / data pairs)
[ Download the ZIP ]

The following program has the following structure:

externalVar1=This is a sentence loaded from an external file.
&externalVar2=43
&externalVar3=This is different sentence.

// creating an instance of LoadVars, used to load external variables
var ext_text = new LoadVars();

// this is saying: "call the addText function when finished loading the data"
ext_text.onLoad = addText;

// begin loading data in from a file called variable_list.txt
ext_text.load("variable_list.txt");

function addText() {
	// setting textbox information from variables loaded
	mytext.htmlText = this.externalVar3;
}


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>

class Spot extends MovieClip {
	var spotState:Boolean;
	var pos:Number;

	function Spot() {
		this.spotState = false;
	}

	function init(pos:Number) {
		var xPos = pos % _root.GRIDWIDTH;
		var yPos = Math.floor(pos / _root.GRIDWIDTH);

		this._x = 70+(xPos*10);
		this._y = 70+(yPos*10);
	}

	function setState(newState:Boolean) {
		spotState = newState;
		if (spotState) this.gotoAndStop("on");
		else this.gotoAndStop("off");
	}

	function switchState() {
		spotState = !spotState;
		if (spotState) this.gotoAndStop("on");
		else this.gotoAndStop("off");
	}
}

stop();

var gridData:Array;
var GRIDHEIGHT = 6;
var GRIDWIDTH = 6;

// loading external data from a file called gridData.xml
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.contentType = "text/xml";
xmlData.load("gridData.xml");

// a function that is automatically called when the data is loaded
xmlData.onLoad = function() {
	var rawGridData = (xmlData.firstChild.firstChild).toString()
	gridData = rawGridData.split(",");

	var boundaries = new Array();
	var bndCt = 0;

	for(var i=0;i<gridData.length;i++) {
		// if there is a 1, create a Spot
		if (gridData[i] == 1) {
			boundaries[bndCt] = attachMovie("spot", "spot" + bndCt, _root.getNextHighestDepth());
			boundaries[bndCt].init(i);
			boundaries[bndCt].setState(true);
			bndCt++;
		}
	}
}