Skip to main content
Participating Frequently
November 28, 2015
Question

Last movie clip in array hitTestObject

  • November 28, 2015
  • 1 reply
  • 514 views

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);

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
November 28, 2015

It is hard to tell if you are confusing the "currentFruit" variable with the "fruitOnStage" array.  The code you show at the start does not appear to relate to the code you show elsewhere... how does it relate?

The way you are declaring the variables within functions ends up making them local to those functions, so they are not going to be visible to any other functions that might be trying to use them.

morrism35Author
Participating Frequently
November 28, 2015

The code below test whether the currentFruit in the array has hit the basket. The way I have it coded works perfectly as you see it written. The wrinkle that I was supposed to add was the basket hitting the Circle element which would result in a point loss.

if(currentFruit.hitTestObject(basket_mc)){

fruitsCollected++;

removeChild(currentFruit);

fruitsOnstage.splice(i,1);

field1_txt.text = "Total Fruit Collected: " +

fruitsCollected;

morrism35Author
Participating Frequently
November 28, 2015

The way the code is written all the 20 pieces of fruit falls from the tree and is caught in a basket resulting in points for catches.