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
All of the following programs have the following structure:
function keyDownHandler(event:KeyboardEvent):void {
if (!guy.hitTestObject(obstacle)) {
if(event.keyCode == Keyboard.RIGHT) {
guy.x += 5;
}
if (event.keyCode == Keyboard.LEFT) {
guy.x -= 5;
}
if(event.keyCode == Keyboard.UP) {
guy.y -= 5;
}
if (event.keyCode == Keyboard.DOWN) {
guy.y += 5;
}
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
[ First click inside the rectangle, then press the left, right, up and down arrow keys ]
function keyDownHandler(event:KeyboardEvent):void {
var prevXPos:Number = guy.x;
var prevYPos:Number = guy.y;
if (!guy.hitTestObject(obstacle)) {
if(event.keyCode == Keyboard.RIGHT) {
guy.x += 5;
}
if (event.keyCode == Keyboard.LEFT) {
guy.x -= 5;
}
if(event.keyCode == Keyboard.UP) {
guy.y -= 5;
}
if (event.keyCode == Keyboard.DOWN) {
guy.y += 5;
}
}
if (guy.hitTestObject(obstacle)) {
guy.x = prevXPos;
guy.y = prevYPos;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
[ First click inside the rectangle, then press the left, right, up and down arrow keys ]
var speed:Number = 5;
function keyDownHandler(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
guy.x += speed;
if (guy.hitTestObject(obstacle)) guy.x = obstacle.x - guy.width - 1;
}
else if (event.keyCode == Keyboard.LEFT) {
guy.x -= speed;
if (guy.hitTestObject(obstacle)) guy.x = obstacle.x + obstacle.width + 1;
}
else if (event.keyCode == Keyboard.UP) {
guy.y -= speed;
if (guy.hitTestObject(obstacle)) guy.y = obstacle.y + obstacle.height + 1;
}
else if (event.keyCode == Keyboard.DOWN) {
guy.y += speed;
if (guy.hitTestObject(obstacle)) guy.y = obstacle.y - guy.height - 1;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
[ First click inside the rectangle, then press the left, right, up and down arrow keys ]
var speed:Number = 5;
function keyDownHandler(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
guy.x += speed;
if (isHit(guy, obstacle)) guy.x = obstacle.x - guy.width;
}
else if (event.keyCode == Keyboard.LEFT) {
guy.x -= speed;
if (isHit(guy, obstacle)) guy.x = obstacle.x + obstacle.width;
}
else if (event.keyCode == Keyboard.UP) {
guy.y -= speed;
if (isHit(guy, obstacle)) guy.y = obstacle.y + obstacle.height;
}
else if (event.keyCode == Keyboard.DOWN) {
guy.y += speed;
if (isHit(guy, obstacle)) guy.y = obstacle.y - guy.height;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function isHit(mc1:MovieClip, mc2:MovieClip):Boolean {
var bounds_obj1:Rectangle = mc1.getBounds(this);
var bounds_obj2:Rectangle = mc2.getBounds(this);
if ((bounds_obj1.right > bounds_obj2.left && bounds_obj1.left < bounds_obj2.right ) && (bounds_obj1.bottom > bounds_obj2.top && bounds_obj1.top < bounds_obj2.bottom)) return true;
return false;
}
[ First click inside the rectangle, then press the left, right, up and down arrow keys ]