Skip to main content
Participating Frequently
November 24, 2015
Question

Restart Button

  • November 24, 2015
  • 1 reply
  • 273 views

I'm very new to flash and i'm on chapter 7 on actionscript classroom in a book. I have an assignment in that book were i'm supposed to add a restart button to this game that drops fruit off a tree and the user catches it in a basket. I'm not sure if we are allowed to post our code here or not so jump me to bad. There are 15 frames in this project. Not getting a error when i click the restart button, but it is not restarting the program.

var fruitArray:Array = new Array(Apple,Strawberry,Pear,Banana,

Orange);

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

}

}

//Restart button

aButton.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);

function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void

{

  stage.addEventListener(Event.ENTER_FRAME, catchFruit);

  gotoAndStop(1);

}

This topic has been closed for replies.

1 reply

Inspiring
November 24, 2015

The following code is creating the fruits when the game starts right?

You need to repeat it when you want to start the game again, So.. put it inside a function:

function addFruits():void

{

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

}

}

Call the function when the game starts:

addFruits();

The restart function should not be inside the enter frame event, keep it outside then when the game finish add the event listener to the "aButton":

if (fruitsLost >= 20) {

field1_txt.text = "Sorry, you lose. You have lost too much fruit!";

field2_txt.text = "";

stage.removeEventListener(Event.ENTER_FRAME, catchFruit);

aButton.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);

for (var j:int = fruitsOnstage.length-1; j > -1; j--) {

currentFruit = fruitsOnstage;

removeChild(currentFruit);

fruitsOnstage.splice(j,1);

}

}

In the Click function call the "addFruits":

function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void

{

addFruits();

  stage.addEventListener(Event.ENTER_FRAME, catchFruit);

  gotoAndStop(1); // I don't know here if you mean basket_mc.gotoAndStop(1);

//As I think you have to remove the restart button event listener

aButton.removeEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);

}