Error1010: A term is undefined and has no properties
This error has been driving me crazy! The debug won't tell me what term I'm having trouble with. Below is the code, it is a class that defines the movement and behavior of these items I'm creating on stage. I'm getting the error twice, once for the avoidMe function and the other for the checkCollision function. AvoidMe makes these items move away from a movieclip the user controls on stage, and the checkCollision function checks if the movieclip hits any of these items and removes them off the stage. Help me!
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Rectangle;
public class itemBehavior extends MovieClip
{
var aRandomNumber:Number = randomNumber(1,3);
//Rectangle boundaries
var xLocation:Number = 0;
var yLocation:Number = 0;
var widthOfRect:Number = 450;
var heightOfRect:Number = 450;
var rectBoundary:Rectangle = new Rectangle(xLocation,yLocation,widthOfRect,heightOfRect);
var itemSpeed:Number = 5;
var minDistanceBetweenItems:Number = 20;
public function itemBehavior():void
{
//Have each item have a random position on the stage
this.x = Math.round(Math.random() * 500) + 20;
this.y = Math.round(Math.random() * 500) + 20;
//Have each item have a different size
this.scaleX = scaleX * aRandomNumber;
this.scaleY = scaleY * aRandomNumber;
//Add listener to move items away from cat
addEventListener(Event.ENTER_FRAME,avoidCat);
//Add listener to remove items from stage when they hit the cat
addEventListener(Event.ENTER_FRAME,checkCollision);
}
function randomNumber(min:Number, max:Number):Number
{
return Math.floor(Math.random() * (1 + max - min) + min);
}
function avoidCat(event:Event):void
{
// Calculate the distance between the cat and the items
var distanceX:Number = MovieClip(parent).cat_mc.x - this.x;
var distanceY:Number = MovieClip(parent).cat_mc.y - this.y;
var distanceXY:Number = Math.sqrt(distanceX*distanceX+distanceY*distanceY);
// Check if distance is small enough
if (distanceXY <= minDistanceBetweenItems)
{
// Calculate direction between cat and the items
var currDirection:Number = Math.atan2(distanceY,distanceX);
// Move items to opposite direction
this.x -= itemSpeed * Math.cos(currDirection);
this.y -= itemSpeed * Math.sin(currDirection);
}
//Restrict items in rectangle boundaries
if (this.x < xLocation)
{
this.x = widthOfRect;
}
if (this.x > widthOfRect)
{
this.x = xLocation;
}
if (this.y < yLocation)
{
this.y = heightOfRect;
}
if (this.y > heightOfRect)
{
this.y = yLocation;
}
}
//Function to check for collision and remove item
function checkCollision(event:Event):void
{
if (MovieClip(parent).cat_mc.hitTestObject(this))
{
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,checkCollision);
}
}
}
}
