Last movie clip in array hitTestObject
I want the last movie clip in my array when it hits the basket to lose one point. I thought i could use the if statement with the currentFruit array
and just have a different code execute when index 5 comes up which would point to the Circle movie clip, but this produces a run time error
of error 1101 undefined:
if(currentFruit[5].hitTestObject(basket_mc)){
fruitsCollected--;
removeChild(currentFruit);
fruitsOnstage.splice(i,1);
field1_txt.text = "Total Fruit Collected: " +
fruitsCollected;
_____________________________________
import flash.display.MovieClip;
import fl.controls.Button;
aButton.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);
function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
var fruitArray:Array = new Array(Apple,Strawberry,Pear,Banana,
Orange,Circle);
var fruitsOnstage:Array = new Array();
var fruitsCollected:int = 0;
var fruitsLost:int = 0;
for (var i:int = 0; i<20; i++) {
var pickFruit = fruitArray[int(Math.random() * fruitArray.
length)];
var fruit:MovieClip = new pickFruit();
addChild(fruit);
fruit.x = Math.random() * stage.stageWidth;
fruit.y = Math.random() * -500;
fruit.speed = Math.random() * 15 + 5;
fruitsOnstage.push(fruit);
}
basket_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragBasket);
stage.addEventListener(MouseEvent.MOUSE_UP, dragStop);
function dragBasket(e:Event):void {
basket_mc.startDrag();
}
function dragStop(e:Event):void {
basket_mc.stopDrag();
}
stage.addEventListener(Event.ENTER_FRAME, catchFruit);
function catchFruit(e:Event):void {
for (var i:int = fruitsOnstage.length-1; i > -1; i--) {
var currentFruit:MovieClip = fruitsOnstage;
currentFruit.y += currentFruit.speed;
if (currentFruit.y > stage.stageHeight - currentFruit.
height) {
currentFruit.y = 0 - currentFruit.height;
fruitsLost++;
field2_txt.text = "Total Fruit Lost: " + fruitsLost;
}
if(currentFruit.hitTestObject(basket_mc)){
fruitsCollected++;
removeChild(currentFruit);
fruitsOnstage.splice(i,1);
field1_txt.text = "Total Fruit Collected: " +
fruitsCollected;
if (fruitsCollected >= 20) {
basket_mc.gotoAndStop(20);
} else if (fruitsCollected > 15) {
basket_mc.gotoAndStop(15);
} else if (fruitsCollected > 10) {
basket_mc.gotoAndStop(10);
} else if (fruitsCollected > 5) {
basket_mc.gotoAndStop(5);
}
}
}
if (fruitsOnstage.length <= 0) {
field1_txt.text = "You Win! You have collected enough fruit for dinner.";
field2_txt.text = "";
stage.removeEventListener(Event.ENTER_FRAME, catchFruit);
}
if (fruitsLost >= 20) {
field1_txt.text = "Sorry, you lose. You have lost too much fruit!";
field2_txt.text = "";
stage.removeEventListener(Event.ENTER_FRAME, catchFruit);
for (var j:int = fruitsOnstage.length-1; j > -1; j--) {
currentFruit = fruitsOnstage
removeChild(currentFruit);
fruitsOnstage.splice(j,1);
