21 Replies Latest reply on Aug 21, 2009 10:24 AM by Mr.Wallace

    Show a button...

    Mr.Wallace Level 1

      Hey all,

       

      I'm am trying to load a button onto the stage when a movie clip (already on stage) reaches a certain frame.

       

      For example, when the movie clip (called 'movie') reaches frame 93 (also labelled 'done'), the 'button' will show up on the stage.

       

      The code I've tried to put together looks something like this:

       

      if (!movie.done) {
      this.attachMovie('button', 'button', this.getNextHighestDepth());
      button._x = Stage.width/2;
      button._y = Stage.height/1.2
      }

      ;

       

      However, when I run this, it displays the 'button' right away, before the 'movie' reaches frame 93.

       

      Any ideas?

        • 1. Re: Show a button...
          Ned Murphy Adobe Community Professional & MVP

          movie.done is meaningless (false) if done is a frame label, and !movie.done is likely treated as NOT meaningless (aka: true), so it will fire off right away.

           

          At frame 93 of the movieclip have it call a function on the main timeline that makes the button appear.

          1 person found this helpful
          • 2. Re: Show a button...
            Mr.Wallace Level 1

            Ok,

             

            First, thanks Ned. That was a little more obvious then I was making it to be.

             

            Secondly...

             

            Now that I have that button showing on the stage I need it to do somethings for me.

             

            Inside that previously discussed movie clip, are two (named) varibles, 'var_one' and 'var_two'. When clicking 'button' I need it to submit those variables to an .ASP file and then go to a different page afterwards. I have virtually copied a previous project scripts so I know that the submitting function works fine. But for some reason, it's not working here. Granted I have only used a part of the scripting from the previous project but it is the needed the part.

             

            The script looks like this:

             

            if (!button) {
            button._x = Stage.width/2;
            button._y = Stage.height/2;
            button.onPress = function() {
              movie.loadVariables("submit.asp", "GET");
              getURL(http://www.google.com, "_parent");
            }
            };

             

            Like I said, I copied this part of code from different project so it is possible I'm missing some things.

            • 3. Re: Show a button...
              kglad Adobe Community Professional & MVP

              your if-statement doesn't make sense.  you're checking if button doesn't exist and then, if it doesn't exist, you're (trying to) define properities and an onPress method.

               

              remove that ! (=not operator).

              • 4. Re: Show a button...
                Mr.Wallace Level 1

                So, it's just the exact same script but without the '!' ?

                 

                Does this even have to be an 'if' statement in the first place?

                • 5. Re: Show a button...
                  kglad Adobe Community Professional & MVP

                  yes, but even that doesn't make a lot of sense.  either you KNOW that button exists and your if-statement is unneeded or it may not exist and you should be doing something to create it or wait for it to exist before trying to assign properties and methods.

                  • 6. Re: Show a button...
                    Mr.Wallace Level 1

                    Yea, see that's what I meant. So I shouldn't even use an 'if' statement.

                     

                    I have successfully gotten the 'submit' button to appear on the stage at the correct time. I obviously can't code the button itself because it isn't even on the stage to begin with. So I have to place a script into the main timeline that will tell that button what to do.

                     

                    if (submit) {
                      this.attachMovie('submit', 'submit', this.getNextHighestDepth());
                    //the script below is just to tell the button where to go on the stage

                      submit._x = Stage.width/2;
                      submit._y = Stage.height/2;
                    //this is the part that is important

                      submit.onPress = function() {
                       score.loadVariables("submit.asp", "GET");
                       getURL(http://www.google.com, "_parent");
                      }
                      };
                    }

                     

                    If I don't need an 'if' statement, then what can I use?

                    • 7. Re: Show a button...
                      kglad Adobe Community Professional & MVP

                      the code you just showed is substantially different from your first code attempt.  you're creating a submit mc and that only makes sense if there's NOT one already.  so, in this situation you should be using the not operator:

                       


                       

                      if (!submit) {
                        this.attachMovie('submit', 'submit', this.getNextHighestDepth());
                      //the script below is just to tell the button where to go on the stage

                        submit._x = Stage.width/2;
                        submit._y = Stage.height/2;
                      //this is the part that is important

                        submit.onPress = function() {
                         score.loadVariables("submit.asp", "GET");
                         getURL(http://www.google.com, "_parent");
                        }
                        };
                      }


                      • 8. Re: Show a button...
                        Mr.Wallace Level 1

                        So then that's the problem, I'm NOT using a movie clip.

                         

                        I have the main stage with a movie clip (called 'score') on it that has 93 frames inside that clip and then the button that is in the library, not on stage.

                         

                        I have coded the 93rd frame inside the clip so that when the playhead reaches it (frame 93) it drops the 'submit' button onto the main stage. There are also two variables called 'name' and 'score' inside the clip (on frame 93) that are going to be submitted by the 'submit' button. I already know that it's possible to submit these variables while inside the 'score' movie clip because it works on a previous project. However, I can't duplicate that code because this project is much smaller in scale, so I copied a part of the code that I used for it.

                         

                        I need to insert a script onto frame 1 of the main timeline that will talk to the 'submit' button now that it's on the stage and tell it to submit the 2 variables to the .ASP page and then go to a new URL.

                         

                        This is the important stuff I need it to do:

                         

                        submit.onPress = function() {
                        score.loadVariables("submit.asp", "GET");
                        getURL(http://www.google.com, "_parent");

                         

                        I'm thinking an 'if' statement is not the way to go here?

                        • 9. Re: Show a button...
                          kglad Adobe Community Professional & MVP

                          code in frame 1 of your main timeline will execute before your submit button exists so you must that code in a function and only call that function after your submit button exists.

                           

                          you should use the loadvars class to send variables to your asp script.  so if your 93 frame movieclip has instance name mc93, you can use:

                           

                          function submitF(){

                           

                          submit.onPress = function() {

                          var sendLV:LoadVars=new LoadVars();

                          sendLV.score = mc93.score;
                          sendLV.send("submit.asp","GET");
                          getURL(http://www.google.com, "_parent");

                           

                          }

                          • 10. Re: Show a button...
                            Mr.Wallace Level 1

                            Dang, it looks like this just isn't going to work. I have copied your suggested script to the "t" and it still just sits there and does nothing after the submit button is pressed.

                             

                            The only change I made was to the frame name which is labelled 'done', so it looks like this now:

                             

                            function submitF(){

                             

                            submit.onPress = function() {

                            var sendLV:LoadVars=new LoadVars();

                            sendLV.score = done.score;
                            sendLV.send("submit.asp","GET");
                            getURL(http://www.google.com, "_parent");

                             

                            }

                            • 11. Re: Show a button...
                              NSurveyor Level 2

                              You should have the code kglad provided (function submitF(){...}) on frame 1 just as you wanted. This function, when called, creates the onPress event handler. However, you actually have to call the function for it to be useful. So, on frame 93, you will want to have submitF(); This effectively circumvents the problem of trying to define the onPress event handle prior to the button's existence.

                              • 12. Re: Show a button...
                                Mr.Wallace Level 1

                                Thanks NS for the input but the submit button is still not showing up on the stage. I made a mistake earlier by saying it was showing up but just not working, it actually isn't showing up at all.

                                 

                                I have a question...

                                 

                                How does the 'submit' button KNOW where to go on the stage? Is it just going to be placed in the middle of the stage by default? Maybe it just doesn't know where to go?!

                                 

                                P.S. kglad, check your PM. Thanks!

                                • 13. Re: Show a button...
                                  Mr.Wallace Level 1

                                  Ok, so clearly my computer is having issues because I can't send PM's. So, I've attached the .FLA file for you to check out. Don't know what else to do...

                                   

                                  P.S. There is a .SWF file called 'Clock' that you won't have but you don't need it.

                                  • 14. Re: Show a button...
                                    NSurveyor Level 2

                                    The submit button knows where to go because you have it centre itself on the screen with these line:

                                     

                                      submit._x = Stage.width/2;
                                      submit._y = Stage.height/2;

                                    It would be helpful if you attached your  *.fla, not your *.swf.

                                    • 15. Re: Show a button...
                                      Mr.Wallace Level 1

                                      Yea...the computer froze before it finished. Let me try again...

                                      • 16. Re: Show a button...
                                        Mr.Wallace Level 1

                                        Jeez...my work system won't allow me load the file because of "security issues".

                                         

                                         

                                         

                                        Try this: http://www.6ix8ightdesigns.com/New/BigBrain.zip

                                         

                                        P.S. Don't mind the little design flaws. I haven't changed everything because I want to get this working before I redo all the little stuff.

                                        • 17. Re: Show a button...
                                          NSurveyor Level 2

                                          Your setup is rather confusing. The reason why the button wasn't showing up at all, was because of a scoping issue. If you have a function defined at the _root level, you cannot access it from within a MovieClip, unless you specify a path to that function, ie _parent.submitF(); or _root.parentF(); This also seemed to fail because you are attaching the button to your score MovieClip. It is not set up so that the registration point is at the top-left, so using the centring you code simply will not give the desired results, in fact positioning it off-screen.

                                           

                                          As far as I can see, for your purposes, you should not attach the submit button through ActionScript. Click on frame 93, and drag your submit button from the Library. Position however you like. In the Properties panel, give it the instance name, submit. Then, in the frames action (still on frame 93), add:

                                           

                                          submit.onPress = function() {
                                           var sendLV:LoadVars = new LoadVars();
                                           sendLV.name = name;
                                           sendLV.score = score;
                                           sendLV.send("submit.asp","GET");
                                           getURL("http://www.google.com", "_parent");
                                          };
                                          
                                          1 person found this helpful
                                          • 18. Re: Show a button...
                                            Mr.Wallace Level 1

                                            Ok, there is some progress.

                                             

                                            It is now communicating with the .ASP page...SWEET! However, it is not passing the variables. I've tried a couple different setups of the script, but nothing do it.

                                            • 19. Re: Show a button...
                                              NSurveyor Level 2

                                              Oh, whoops, it appears we have been using the LoadVars.send(); method incorrectly all this time. When using send(), it is necessary to specify a target frame for the results to open up in. What we have is the results opening up in a window named "GET", and because we did not properly specify the method, "POST" by default is being used.

                                               

                                              To remedy, simply replace:

                                              sendLV.send("submit.asp","GET");

                                               

                                              with:

                                              sendLV.send("submit.asp","_blank","GET"); 

                                               

                                              Instead of _blank, you can also specify: _top/_parent, _self, or a named frame. If you do not want a window to open, consider using LoadVars.sendAndLoad(); as opposed to LoadVars.send();

                                              • 20. Re: Show a button...
                                                Mr.Wallace Level 1

                                                That actually caused it not to communicate at all again.

                                                 

                                                In the other project I created the code looked like this:

                                                 

                                                submit.onPress = function() {
                                                score.loadVariables("submit.asp", "GET");
                                                getURL(http://www.google.com, "_parent");
                                                }

                                                 

                                                Just wondering why you changed it to:

                                                 

                                                submit.onPress = function() {
                                                var sendLV:LoadVars = new LoadVars();
                                                sendLV.name = name;
                                                sendLV.score = score;
                                                sendLV.send("submit.asp", "GET");
                                                getURL(http://www.google.com, "_parent");
                                                };

                                                 

                                                ?

                                                 

                                                Is it because the 'submit' button is now found inside of the 'score' movie clip instead of landing on the main stage and telling the 'score' clip what to do?

                                                • 21. Re: Show a button...
                                                  Mr.Wallace Level 1

                                                  Ok, you will all be glad to know that with all of your inputs, I have figured it out using various info I've gotten from you guys and it works well now.

                                                   

                                                  Thank you guys for ALL of your help, I honestly had ZERO chance of figuring this out without your help!!