12 Replies Latest reply: Nov 16, 2011 2:24 AM by Peter Celuch RSS

    Replay doesn't work on first click

    EGBAR.Mary Community Member

      I have a single Flash layer with Action Script and I have a replay button, but it takes 2 clicks to make it replay. Does anyone know why?

       

       

      buttonReplay.addEventListener(MouseEvent.CLICK, playAgain);

       

       

      function playAgain(event:MouseEvent){

                removeEventListener(Event.ENTER_FRAME,moveGift);

                onSnow.removeEventListener(MouseEvent.CLICK, decorationsOn);

                star.removeEventListener(MouseEvent.CLICK, lightsOn);

                bowButton.removeEventListener(MouseEvent.CLICK, surprise);

         myChannel.stop();

         gotoAndPlay(1);

      }

        • 1. Re: Replay doesn't work on first click
          Peter Celuch Community Member

          Does the flashplayer have the focus already? Other buttons work on first click? I don't see anything suspicious in your code.

          • 2. Re: Replay doesn't work on first click
            EGBAR.Mary Community Member

            Everything else works on the first click.

            • 3. Re: Replay doesn't work on first click
              Peter Celuch Community Member

              I would say that you have something in front of the button.. some invisible object.

               

              1) Download this class: Utils.as

              2) Unpack it into your project's folder, right next to the fla.

              3) Paste this code into your project:

               

              import eu.flashlabs.utils.Utils;
              Utils.startTracingObjectsUnderCursor(stage, trace);
              

               

              Now when you click wherever on the stage, you'll get listing of objects that are under the cursor in their z-order. So you can investigate what are you clicking on exactly.

              • 4. Re: Replay doesn't work on first click
                EGBAR.Mary Community Member

                I clicked on each item and here are the codes. The first three work fine, the Sprite object is the Replay.

                 

                 

                trace objects under cursor:

                [object bigStar] instance7[ 54.6, 0, 46.35, 47.2 ]

                           -> [object ballsSnow] instance6[ 510, 450, 178.9, 93.85 ]

                                     -> [object MainTimeline] root1[ 0, 0, 800, 601.35 ]

                                               -> [object Stage] null[ 0, 0, 800, 601.35 ]

                ----------------------------

                trace objects under cursor:

                [object bigStar] instance23[ 620, 450, 46.35, 47.2 ]

                           -> [object MainTimeline] root1[ 0, 0, 800, 601.35 ]

                                     -> [object Stage] null[ 0, 0, 800, 601.35 ]

                ----------------------------

                trace objects under cursor:

                [object bow] instance73[ 630, 530, 57.95, 48.15 ]

                           -> [object MainTimeline] root1[ 0, 0, 800, 601.35 ]

                                     -> [object Stage] null[ 0, 0, 800, 601.35 ]

                ----------------------------

                trace objects under cursor:

                [object Sprite] instance76[ 0, 0, 50, 23 ]

                           -> [object MainTimeline] root1[ 0, 0, 800, 601.35 ]

                                     -> [object Stage] null[ 0, 0, 800, 601.35 ]

                ----------------------------

                • 5. Re: Replay doesn't work on first click
                  Peter Celuch Community Member

                  The last one is different. You clicked on Sprite with these properties:

                       x: 0

                       y: 0

                       width: 50

                       height: 23

                   

                  It's positioned right at the main timeline. So you should now go to your fla, scrub timeline to frame you were looking when you clicked, unlock and unhide all layers and start pressing TAB. Look at the properties panel which item fits the description. It should be in higher layer than your button You want to click.

                  • 6. Re: Replay doesn't work on first click
                    EGBAR.Mary Community Member

                    This is the code for the replay button:

                     

                    var buttonReplay:Sprite;

                              buttonReplay = new Sprite();

                              buttonReplay.graphics.beginFill(0xFFF000);

                              buttonReplay.graphics.drawRoundRect(733, 563, 50, 23, 15);

                              buttonReplay.graphics.endFill();

                               buttonReplay.buttonMode = true;

                               buttonReplay.mouseChildren = false;

                              var replay:TextField = new TextField();

                              replay.mouseEnabled = false;

                              replay.autoSize = TextFieldAutoSize.LEFT;

                              replay.text = "Replay";

                              replay.x = 740;

                              replay.y = 565;

                              replay.setTextFormat(buttonStyle);

                              addChild(buttonReplay);

                              addChild(replay);

                    • 7. Re: Replay doesn't work on first click
                      EGBAR.Mary Community Member

                      So I changed it to my button to:

                       

                      var buttonReplay: replayButton = new replayButton();

                                buttonReplay.x = 738;

                                buttonReplay.y = 566;

                                buttonReplay.buttonMode = true;

                       

                       

                                var replay:TextField = new TextField();

                                replay.mouseEnabled = false;

                                replay.autoSize = TextFieldAutoSize.LEFT;

                                replay.text = "Replay";

                                replay.x = 735;

                                replay.y = 561;

                                replay.setTextFormat(buttonStyle);

                                addChild(buttonReplay);

                                addChild(replay);

                       

                      and the trace reads:

                      trace objects under cursor:

                      [object replayButton] instance71[ 738, 566, 51.85, 18.3 ]

                                 -> [object MainTimeline] root1[ 0, 0, 800, 631.2 ]

                                           -> [object Stage] null[ 0, 0, 800, 631.2 ]

                       

                      I cannot scrub the timeline, because all my action script is on the 1st frame, and only one layer. So is it wrong to write ," gotoAndPlay(1);"?

                      • 8. Re: Replay doesn't work on first click
                        Peter Celuch Community Member

                        It's ok to have one frame and one layer. You don't need to write no gotoAndPlay..

                        According to the output it seems that the button is now working correctly, right?

                        • 9. Re: Replay doesn't work on first click
                          EGBAR.Mary Community Member

                          No, it still requires a double-click to start again. I I leave out the gotoAndPlay, how does it know to start over?

                          • 10. Re: Replay doesn't work on first click
                            Peter Celuch Community Member

                            Don't you have stop() or something on the first frame too? I'm quite sure the problem is NOT in the button. If you add trace("PLAY AGAIN!"); to the function playAgain(), I'm sure it will trace everytime you click. If you have initialization scripts on frame 1, you shouldn't go back there to repat the animation because all the initializations will run again. Try changing gotoAndPlay(1) to gotoAndPlay(2) for starters. It might work.

                             

                            If no, isolate all the initialization code to first frame and everything that is bound to animation start, place to frame 2.

                            • 11. Re: Replay doesn't work on first click
                              EGBAR.Mary Community Member

                              Thank you!!!!

                               

                              I left all the initialization code to first frame and everything that has  animation went to a new keyframe on frame 2.

                               

                              Again, thank you for all your patience and suggestions!

                              • 12. Re: Replay doesn't work on first click
                                Peter Celuch Community Member

                                You're welcome.