As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

ActionScript 3 - How to make a Replay Button?

Cowboy BebopCowboy Bebop Registered User regular
edited April 2010 in Help / Advice Forum
Basically I can't get my head around actionscript 3 as I'm so used to using actionscript 2 in the past. All the button needs to do is go back and play my animation from the first Scene.

Thanks in advance I've been pulling my hair out all day watching a million youtube videos and scanning every site I can find to no avail.

Cowboy Bebop on

Posts

  • Options
    robotbeboprobotbebop Registered User regular
    edited April 2010
    What specifically doesn't work? you should be able to create a Button symbol and then instead of using AS2's god fucking awful syntax, you would just do something like below, assuming the Button symbol's instance on the stage is called MyButton -
    function MyButtonClicked(evt:MouseEventArgs):void
    {
        gotoAndPlay(1);
    }
    
    MyButton.addEventListener(MouseEvent.CLICK, MyButtonClicked);
    

    I can't remember off the top of my head of CLICK is a member from Event or MouseEvent though. But that's the jist of it.

    robotbebop on
    Do not feel trapped by the need to achieve anything, this way you achieve everything.

    Oh, hey I'm making a game! Check it out: Dr. Weirdo!
  • Options
    Cowboy BebopCowboy Bebop Registered User regular
    edited April 2010
    I already tried that but the button seemed dead, perhaps it was just going to frame 1 of the replay scene?

    Cowboy Bebop on
  • Options
    robotbeboprobotbebop Registered User regular
    edited April 2010
    I already tried that but the button seemed dead, perhaps it was just going to frame 1 of the replay scene?

    where are you putting that code ? In the Button's script or the main timeline script?

    robotbebop on
    Do not feel trapped by the need to achieve anything, this way you achieve everything.

    Oh, hey I'm making a game! Check it out: Dr. Weirdo!
  • Options
    Cowboy BebopCowboy Bebop Registered User regular
    edited April 2010
    My animation is spread over a couple scenes and on the last scene I want there to be a replay button. I wish I had just done it all on the one timeline now just because it's made my life hell now.

    Cowboy Bebop on
  • Options
    robotbeboprobotbebop Registered User regular
    edited April 2010
    If you put the code in the scene's timeline somewhere it should work

    See Adobe's Reference Doc for more info.

    robotbebop on
    Do not feel trapped by the need to achieve anything, this way you achieve everything.

    Oh, hey I'm making a game! Check it out: Dr. Weirdo!
  • Options
    Cowboy BebopCowboy Bebop Registered User regular
    edited April 2010
    Ah the link looks promising thanks for the help robot

    Cowboy Bebop on
  • Options
    schweigerjschweigerj Registered User new member
    very new to this site/forum, also can't get my replay button to work. Everything is on frame1. This is a game where fruit falls from the top of the stage (static image of a tree in the background so it looks like the fruit is coming out of the tree) and you try and catch 20 instances of the fruit in the basket before 20 instances fall to the "ground"/below the stage. The replay text and button is visible when the game ends. However, I can't get the button to start the code over and start the game again. I put the last block of code inside the button function which just made the fruit accumulate at the top of the stage every time I clicked the replay_btn, which makes sense now as I look at the code, but there has to be a simple way to get the fruit to drop again...


    import flash.display.SimpleButton;
    import flash.display.MovieClip;
    import flash.events.Event;

    var fruitArray:Array = new Array(Apple,Strawberry,Pear,Banana,Orange);
    var fruitsOnstage:Array = new Array();

    var fruitsCollected:int = 0;
    var fruitsLost:int = 0;

    replay_btn.visible = false;
    field3_txt.visible = false;

    for(var i:int = 0; i < 20; i++)
    {
    var pickFruit = fruitArray[int(Math.random() * 5)];
    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);
    // ** book says this is where you would enter the option to replay the game**
    }
    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[j];
    removeChild(currentFruit);
    fruitsOnstage.splice(j,1);
    replay_btn.visible = true;
    field3_txt.visible = true;
    }
    }
    }

    replay_btn.addEventListener(MouseEvent.CLICK, replay);
    function replay(e:Event):void
    {
    if (fruitsLost >= 20)
    {
    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() * 5)];
    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);
    }

This discussion has been closed.