So, what I did was create a House.as package/class that created the cats and birds, and handled the collision detection to determine if a cat hit another cat or a bird hit another bird (the collision was the "event" that I used to drive the actions in the house).
I was going to have a text field pop up next to the animal to show the Meow or Chirp, but ended up just doing it in the trace statement because I wasn't sure if I should try to create the text field directly within the collision detection, or create a function within the Cat/Bird script and call that from the collision function...
Here's House.as:
Actionscript Code:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Loader;
public class House extends MovieClip
{
private var cats:Array;
private var birds:Array;
private var numCats:Number = 2;
private var numBirds:Number = 2;
public function House() {
init();
}
private function init() {
makeCats();
makeBirds();
addEventListener(Event.ENTER_FRAME, catCollision);
addEventListener(Event.ENTER_FRAME, birdCollision);
}
public function makeCats() {
cats = new Array();
for (var i:int = 0; i < numCats; i++) {
var cat:Cat = new Cat();
cat.x = Math.random() * stage.stageWidth;
cat.y = Math.random() * stage.stageHeight;
addChild(cat);
cats.push(cat);
}
}
public function makeBirds() {
birds = new Array();
for (var i:int = 0; i < numBirds; i++) {
var bird:Bird = new Bird();
bird.x = Math.random() * stage.stageWidth;
bird.y = Math.random() * stage.stageHeight;
addChild(bird);
birds.push(bird);
}
}
private function catCollision(event:Event):void{
for (var i:int = 0; i < cats.length; i++) {
if (cats[0].hitTestObject(cats[1])) {
trace("meow");
}
}
}
private function birdCollision(event:Event):void{
for (var i:int = 0; i < birds.length; i++) {
if (birds[0].hitTestObject(birds[1])) {
trace("chirp");
}
}
}
}
}
Then I created a Cat.as and Bird.as to load graphics for the animals, position them in the house and create random motion to move them around.
Here's Cat.as:
Actionscript Code:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.text.*;
public class Cat extends Sprite
{
var imageLoader:Loader = new Loader();
private var vx:Number = Math.random() * 8 - 4;
private var vy:Number = Math.random() * 8 - 4;
var left:Number = 20;
var right:Number = 500;
var top:Number = 20;
var bottom:Number = 350;
public function Cat () {
trace ("cat created");
var image:URLRequest = new URLRequest("kitty.png");
imageLoader.load(image);
this.addChild (imageLoader);
this.addEventListener(Event.ADDED_TO_STAGE, Run);
}
private function Run(e:Event) {
trace("running");
this.addEventListener(Event.ENTER_FRAME, RandomRun);
}
private function RandomRun(e:Event) {
this.x += vx;
this.y += vy;
if (this.x > right) {
this.x = right;
vx *= -1;
}else if (this.x < left) {
this.x = left;
vx *= -1;
}else if (this.y > bottom) {
this.y = bottom;
vy *= -1;
}else if (this.y < top) {
this.y = top;
vy *= -1;
}
}
}
}
and Bird.as:
Actionscript Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Loader;
public class Bird extends MovieClip
{
var imageLoader:Loader = new Loader();
private var vx:Number = Math.random() * 10 - 5;
private var vy:Number = Math.random() * 10 - 5;
var left:Number = 20;
var right:Number = 500;
var top:Number = 20;
var bottom:Number = 350;
public function Bird () {
trace ("bird created");
var image:URLRequest = new URLRequest("bird.png");
imageLoader.load(image);
this.addChild (imageLoader);
this.addEventListener(Event.ADDED_TO_STAGE, Fly);
}
private function Fly(e:Event) {
trace("flying")
this.addEventListener(Event.ENTER_FRAME, RandomFly);
}
private function RandomFly(e:Event) {
this.x += vx;
this.y += vy;
if (this.x > right) {
this.x = right;
vx *= -1;
}else if (this.x < left) {
this.x = left;
vx *= -1;
}else if (this.y > bottom) {
this.y = bottom;
vy *= -1;
}else if (this.y < top) {
this.y = top;
vy *= -1;
}
}
}
}
Thanks for any input/ideas on this!!