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


Collision Detection
(October 8th, 2007)

The following programs have the following structure:

Hit Test Problem
[ Download the FLA ]
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 ]


Hit Test Solution 1
[ Download the FLA ]
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 ]


Hit Test Solution 2
[ Download the FLA ]
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 ]


Hit Test Solution 3
[ Download the FLA ]
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 ]


The following program have the following structure:

Referencing Dynamic Movie Clips
[ Download the FLA ]
// HOW DO YOU REFERENCE DYNAMICALLY CREATED OBJECTS???

var boxes:Array = new Array(10);

for(var i:Number=0;i<10;i++) {
  var box:Box = new Box();
  addChild(box);
  box.x = Math.random()*stage.stageWidth;
  box.y = Math.random()*stage.stageHeight;
  box.stop();
  
  box.name = "box" + i;
  boxes[i] = box;
  box.id = i;
}

// using the name property
MovieClip(getChildByName("box" + 0)).gotoAndStop("green");

// using an array
boxes[1].gotoAndStop("red")

// using a dynamic instance variable
for(var j:Number = 1;j<this.numChildren;j++) if (Box(this.getChildAt(j)).id == 2) MovieClip(this.getChildAt(j)).gotoAndStop("blue");