11 Replies Latest reply on Oct 23, 2009 8:20 AM by Ned Murphy

    Random Button ActionScript 2?

    pyroasta

      I'm trying to make a random button in my flash game, and nothing I try works. I can't post the file either becausde it's too big. The button is in a movie clip where everything else is. Someone help please.

        • 1. Re: Random Button ActionScript 2?
          Ned Murphy Adobe Community Professional & MVP

          You need to explain what you are trying to do a little better.  What code are you trying to use?  What are the pieces involved? You could probably create a sample file that only demonstrates what you are trying to do.

          • 2. Re: Random Button ActionScript 2?
            pyroasta Level 1

            What I'm trying to do is create a random frame button for adress up game. there are a lot of layers, with multiple frames. i'm trying to make a button that will make each layer go to a random frame everytime you click it.

            • 3. Re: Random Button ActionScript 2?
              Ned Murphy Adobe Community Professional & MVP

              Have you any code to show that you have tried?  How many frames are involved (I don't care about layers)?  How do you have this button incorporated into the timeline?

               

              If you can show the code you have tried, it will make providing a possible solution easier due to the different ways buttons can be coded... it'll save time to offer a solution the way you tried.

              • 4. Re: Random Button ActionScript 2?
                pyroasta Level 1
                > I put this on the main stage of the button; 
                >
                > _global.Head = 1;
                > _global.Beak = 1;
                > _global.Eye = 1;
                > _global.Chest = 1;
                > _global.Body = 1;
                > _global.Tail = 1;
                > _global.RightLimb = 1;
                > _global.LeftLimb = 1;
                >
                > I put this on the button itself;
                >
                > on(release){
                >      _global.Head = (Math.random() * 4) + 1;
                >      _global.Body = (Math.random() * 4) + 1;
                >      _global.Beak = (Math.random() * 4) + 1;
                >      _global.Chest = (Math.random() * 4) + 1;
                >      _global.RightLimb = (Math.random() * 4) + 1;
                >      _global.LeftLimb = (Math.random() * 4) + 1;
                >      _global.Tail = (Math.random() * 4) + 1;
                >      _global.Eye = (Math.random() * 6) + 1;
                > }
                and I put this code on all the pieces 
                >
                > onClipEvent(enterFrame){//when the frame updates
                >      _root.insertpiecehereframe.text = parseInt(_global.body);//set the textbox
                >      this.gotoAndStop(parseInt(_root.insertpiecehereframe.text));//set the frame
                based on the textbox
                >      }

                 

                 

                Does this help?

                • 5. Re: Random Button ActionScript 2?
                  Ned Murphy Adobe Community Professional & MVP

                  I may end up chasing my tail on this due to what you've described and the code you show.  You say you want different layers to go to different frames.  That is not possible--one timeline, one frame at a time.  So I have to assume you mean you have different movieclips that you want each to go to a different frame within themselves.  If that is the case, then I suggest you let the button code do all the work and get rid of that clipevent code.  But before I outline the solution, I need you to confirm what you are trying to really accomplish.

                   

                  If you could attach your fla file here (zip it or it will not upload) then it may help save alot of time.

                  • 6. Re: Random Button ActionScript 2?
                    pyroasta Level 1

                    Yes, that's is how I meant to word it. I have different movie clips, that i want the button to make them go on different frames every time it's clicked. sadly however I cannot post the actual file. I will post a smaller and similar one though.

                    • 7. Re: Random Button ActionScript 2?
                      Ned Murphy Adobe Community Professional & MVP

                      Here's what I'll suggest--I haven't looked at your file yet.  I do't know if having those _global variables is significant, so I am leaving them in, but if they are only for the purpose of indicating which frames the movieclips should go to, then they can go as well.  In either case, you will get rid of the onclipevent code you have attached to each movieclip

                       

                      For starters, you will need to assign an instance name to each of the movieclips.  Instance names are assigned in the properties panel.  I will use names that reflect the global variables, with _mc appended....

                       

                      on(release){
                           _global.Head = Math.floor(Math.random() * 4) + 1;

                           head_mc.gotoAndStop( _global.Head );
                           _global.Body = Math.floor(Math.random() * 4) + 1;

                            body_mc.gotoAndStop( _global.Body );

                           _global.Beak = Math.floor(Math.random() * 4) + 1;

                            beak_mc.gotoAndStop( _global.Beak );

                       

                          etc....
                      }

                       

                      OR, if you don't need the _globals...

                       

                      on(release){

                            head_mc.gotoAndStop(Math.floor(Math.random() * 4) + 1);

                            body_mc.gotoAndStop(Math.floor(Math.random() * 4) + 1);

                            beak_mc.gotoAndStop(Math.floor(Math.random() * 4) + 1);

                       

                          etc....
                      }

                       

                      Note that I have added Math.floor into the mix.  This produces an integer value out of what the Math.random creates.

                      • 8. Re: Random Button ActionScript 2?
                        pyroasta Level 1

                        neither of the codes seem to be working. I'm applying the code to the button itself, and yet when i click the button, nothing changes.

                        • 9. Re: Random Button ActionScript 2?
                          Ned Murphy Adobe Community Professional & MVP

                          Here's a link to your file (saved as a Flash 8 file) which follows what I described in the previous posting, where I do not use the _global variables at all.  If you click on each of the two movieclips you'll see they each have an instance name assigned in the properties panel.  This name is used to command each one to move to the random frame that gets calculated for each one each time you click the button.  The only object with any code attached to it is the button.

                           

                          http://www.nedwebs.com/Flash/AS2_Random_Select.fla

                          • 10. Re: Random Button ActionScript 2?
                            pyroasta Level 1

                            I figured out what i was doing wrong. I didn't have _mc in the instance name, and that made it not work. I didn't know that wasn't part of the code. thanks for your help.

                            • 11. Re: Random Button ActionScript 2?
                              Ned Murphy Adobe Community Professional & MVP

                              You're welcome