Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I was questioning this code, which is the code you showed relative to the error you were getting:
if(currentFruit[5].hitTestObject(basket_mc)){
fruitsCollected--;
removeChild(currentFruit);
fruitsOnstage.splice(i,1);
field1_txt.text = "Total Fruit Collected: " +
fruitsCollected;
There is no currentFruit[5] based on anything I see.
Can you see in the following code how you declare the fruitsOnStage inside a function?
function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
var fruitArray:Array = new Array(Apple,Strawberry,Pear,Banana,
Orange,Circle);
var fruitsOnstage:Array = new Array();
That array will not be visible outside of that function, so when your catchFruit function tries to use it, it should result in an error. Try declaring it outside the function so that it is visible to all functions that need it.
var fruitsOnstage:Array;
function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
var fruitArray:Array = new Array(Apple,Strawberry,Pear,Banana,Orange,Circle);
fruitsOnstage = new Array();
Copy link to clipboard
Copied
The code below is what I wanted to try so I can isolate the Circle element. If I were to remove the[5] and just run it currentFruit.hitTestObject(basket-mc)
the code runs just fine and the game catches fruit and scores it, so declaring the fruitsonstage array outside of the function made no difference in the original functionality of my code. Now when I use the code below that's when I get the error message.
This is the code to lose a point if the basket collides with the Circle element. This is throwing the error message. Moving the array
outside of the function doesn't do any good. This code and the one below are the same except for one references an array element specifically
and the other doesn't. Both pieces of code are decalred inside the function but only one is throwing an error message. In short moving the code
outside of the function made know difference.
if(currentFruit[5].hitTestObject(basket_mc)){
if(currentFruit.hitTestObject(basket_mc)){
fruitsCollected--;
removeChild(currentFruit);
fruitsOnstage.splice(i,1);
field1_txt.text = "Total Fruit Collected: " +
fruitsCollected;
Original Code All fruit comes down from the tree is caught and scored(Program works perfectly)
if(currentFruit.hitTestObject(basket_mc)){
fruitsCollected++;
removeChild(currentFruit);
fruitsOnstage.splice(i,1);
field1_txt.text = "Total Fruit Collected: " +
fruitsCollected;
Copy link to clipboard
Copied
If you go back to my first reply, and my second reply for that matter, you do not have an array named currentFruit, so trying to use currentFruit[5] is not going to work
Copy link to clipboard
Copied
I get it now currentFruit is just a variable that received its value from the fruitonstage array. got kinda if an ideal now how i would execute the code if the last element collides were i want to take a point from the total. how would you do it. i'm thinking something like this.
if(!currentFruit.hitTestObject(basket_mc))==5{ |
fruitsCollected++;
else if(currentFruit.hitTestObject(basket_mc))==5
fruitsCollected--;