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


An Object Oriented Light (Version 1)
[ Download the ZIP ]

The following program has the following structure:

// defining a class called Light
class Light {
	// a class variable
	var lightState;

	// the class constructor which gets called when you create an object
	// note: a class must have one, and it must be the same name as your Class
	function Light() {
		this.lightState = false;
	}

	// a class method that prints out the light's current state
	function printState() {
		if (lightState) trace("Light is on.");
		else trace("Light is off.");
	}

	// a class method that switches the light's current state
	function switchState() {
		lightState = !lightState;
	}
}

stop();

// declaring a variable light, of type Light, a class we've created
var light:Light;

// creating a light object from our Light class
light = new Light();

// referencing methods defined in our light object using dot syntax
light.printState();
light.switchState();
light.printState();
light.switchState();
light.printState();


This program will print out the following:

Light is off.
Light is on.
Light is off.

An Object Oriented Light (Version 2)
[ Download the ZIP ]

The following program has the following structure:

// defining a class called Light, which extends the MovieClip class
class Light extends MovieClip {
	// a class variable
	var lightState;

	// the class constructor which gets called when you create an object
	// note: in this case, its called when you start the program, and it sees our light MovieClip on the
	// first frame
	function Light() {
		this.lightState = false;

		this.onPress = function() {
			this.switchState();
		}
	}

	// a class method sets the light's current state
	function setState(newState:Boolean) {
		lightState = newState;
		if (newState) this.gotoAndStop("on");
		this.gotoAndStop("off");
	}

	// a class method that switches the light's current state
	function switchState() {
		lightState = !lightState;
		if (lightState) this.gotoAndStop("on");
		else this.gotoAndStop("off");
	}
}

stop();


[ Click on the switch to turn the light on and off. ]

An Object Oriented Light (Version 3)
[ Download the ZIP ]

The following program has the following structure:

// defining a class called Light, which extends the MovieClip class
class Light extends MovieClip {
	// a class variable
	var lightState;

	// the class constructor which gets called when you create an object
	// note: in this case, its called when you create a movieClip using attachMovie
	function Light() {
		this.lightState = false;


		this.onPress = function() {
			this.switchState();
		}
	}

	// a class method that sets the light's position
	// note, it uses the movieClip's class variables _x and _y
	function init(xPos:Number, yPos:Number) {
		this._x = xPos;
		this._y = yPos;
	}

	// a class method sets the light's current state
	function setState(newState:Boolean) {
		lightState = newState;
		if (newState) this.gotoAndStop("on");
		this.gotoAndStop("off");
	}

	// a class method that switches the light's current state
	function switchState() {
		lightState = !lightState;
		if (lightState) this.gotoAndStop("on");
		else this.gotoAndStop("off");
	}
}

stop();

// creating a movie clip from movie clip "light", which is extended by our
// class "Light"
var light = attachMovie("light", "light_1", _root.getNextHighestDepth());

// initializing
light.init(90,65);


[ Click on the switch to turn the light on and off. ]