Copy link to clipboard
Copied
Hi, I'm trying to make a game where the character "eats" candy. So when it collides with the candy, the candy in the array should disappear or be removed. Below is my code for removing. I keep receiving an error message that states "TypeError: Error #1010: A term is undefined and has no properties." Please help me. Thank you.
stage.addEventListener(Event.ENTER_FRAME, duplicateCandy);
var myCandies:Array = new Array();
var candyCount:int = 0;
var candySprite:Sprite;
//"candy" is the linkage name in the library
var myCandy:candy;
function duplicateCandy(candyEvent:Event) {
if (candyCount < 5){
candySprite = new Sprite();
stage.addChild(candySprite);
myCandy = new candy();
candySprite.addChild(myCandy);
candySprite.x = Math.random() * stage.width;
candySprite.y = Math.random() * stage.height;
candySprite.width = 50;
candySprite.height = 50;
var blnColorChange:ColorTransform = new ColorTransform();
blnColorChange.blueOffset = (Math.random() * 510) - 255;
blnColorChange.greenOffset = (Math.random() * 510) - 255;
blnColorChange.redOffset = (Math.random() * 510) - 255;
candySprite.transform.colorTransform = blnColorChange;
myCandies[candyCount] = candySprite;
candyCount += 1;
if(candyCount >= 1){
stage.addEventListener(Event.ENTER_FRAME,moveCandy);
}
}
}
function moveCandy(canEvent:Event){
for(var i:int = 0; i < myCandies.length;i++){
myCandies[i].x -= 10;
if (myCandies[i].x < 0){
myCandies[i].x = stage.width;
}
}
for (var k:int = 0; k < myCandies.length;k++)
{
if (myCandies[k].hitTestObject(om))
{
stage.removeChild(myCandies[k]);
myCandies.splice(k, 1);
}
if(myCandies[k].y > 400)
{
stage.removeChild(myCandies[k]);
myCandies.splice(k, 1)
}
}
}
Copy link to clipboard
Copied
Also, om is the instance name for the character. myCandies is the array of candies.
Copy link to clipboard
Copied
Which Adobe app do you use?