15 Replies Latest reply on Nov 11, 2017 12:57 AM by jasmineeeden

    Creating an action on a button in action script 3 adobe flash CS6

    jasmineeeden

      HI,

       

      I am a newbie to flash, I have however learnt a lot and achieved a lot, including using coding in action script within the past week. However, I have problems with getting a button to link to another scene, it sounds so simple and there are loads of tutorials out there, BUT, i get to the same point every time that doesn't work for me. I have a main page which is the solar system which is coded to be looped every 20seconds - I managed to create invisible motion tweened buttons to follow the video file of the planets, they are clickable and are working but when i try to open the actions panel for the button to actually function, I get the messaged - "in action script 3 code cannot be placed directly on objects" I have tried it all different ways but can't seem to figure it out ! I have tried double clicking on the button and adding a layer within the button for coding, but the same message appears - it also adds another button layer - not timeline layer. I have tried going back to the main scene and adding a layer - but the codes that I've found that everyone is using for scene navigation doesn't work - because how will it know which layer/element I want to divert from.

       

      Basically - I have 10 planets, I want to link the 10 planets to 10 separate scenes, I have 10 buttons which have been motion tweeted and filtered to match the visual of the planets (which i made and imported from after effects) the planets and planet buttons are all on separate layers.

       

      any advice greatly welcome !

       

      Jasmine

       

      [Moved from the non-technical Lounge to the specific Program forum... Mod]

      [Here is the list of all Adobe forums... https://forums.adobe.com/welcome]

        • 1. Re: Creating an action on a button in action script 3 adobe flash CS6
          Colin Holgate MVP & Adobe Community Professional

          In ActionScript 2 you could attach a script to a button, but in ActionScript 3 you create a listener for the click, and then do something with the information.

           

          You can end up with fairly short functions, if you carefully name your buttons. For example, if each button had a name that was the same as the scene you want to go to (eg, button "earth" needs to take you to scene "earth scene"), you could have this script in the first frame of the menu scene:

           

          stage.addEventListener(MouseEvent.CLICK,checkplanet);

           

          function checkplanet(e:MouseEvent){

              if(e.target.name==null || e.target.name=="") return;

               stage.removeEventListener(MouseEvent.CLICK,checkplanet);

              gotoAndPlay(1, e.target.name+" scene");

          }

           

          I'm on a bus as I type this without testing, so watch out for errors! But it should work. If you click not on a button, or on a button that doesn't have a name, nothing should happen. If it's one of the named planet buttons it will combine the button name with " scene" to work out the name of the scene to go to.

          • 2. Re: Creating an action on a button in action script 3 adobe flash CS6
            jasmineeeden Level 1

            Thank You so much for replying !

             

            I tried what you said, it didn't work, I'm not confident I understand you correctly though....

             

            do I just copy and paste the code you provided (as is) onto frame 1 of the scene i want the button to go too....

             

            I am starting with the Sun planet -

             

            so i have 2 layers named -   Sun    (which is the fla video file) and i have    Sun Button (which is the motion tweened button). I have called scene 2 SunScene. I inserted your code but nothings happening ... how does it know I am talking about Sun Button and Sun Scene ...do i change the word Name in your code to SunButton ? so

             

            stage.addEventListener(MouseEvent.CLICK,checkplanet);

             

            function checkplanet(e:MouseEvent){

                if(e.target.SunButton==null || e.target.SunButton=="") return;

                 stage.removeEventListener(MouseEvent.CLICK,checkplanet);

                gotoAndPlay(1, e.target.SunButton+" scene2");

            }

             

            thanks again Colin ! you star !

            Jasmine

            • 3. Re: Creating an action on a button in action script 3 adobe flash CS6
              Colin Holgate MVP & Adobe Community Professional

              This part:

               

              gotoAndPlay(1, e.target.SunButton+" scene2");

               

              would need to be like I wrote it:

               

              gotoAndPlay(1, e.target.name+" scene");

               

              It would then use the name that you had given the button on the stage. That is, you select the first frame where the button starts and look at the Properties panel, and give it a name of "sun", and you would have a scene you have named "sun scene" (you can change the names of scenes, they don't have to remain as Scene 1, Scene , etc).

               

              If you want to post the FLA somewhere I can look at it, I can fix whatever you got wrong, so you can see what I meant.

              • 4. Re: Creating an action on a button in action script 3 adobe flash CS6
                jasmineeeden Level 1

                Hi Colin,

                 

                thank you so much for this i really appreciate it !

                 

                I still can't get it too work :-/ I tried what you said, I have no idea where I am going wrong.

                 

                when you say post the .fla somewhere do you mean something like Wetransfer? Because I'de need your email address ? or i can share a dropbox link? Thanks so much for offering.....

                 

                 

                Jasmine

                • 5. Re: Creating an action on a button in action script 3 adobe flash CS6
                  jasmineeeden Level 1

                  here is the dropbox link - https://www.dropbox.com/s/gxoyhylbn4xuql3/StarmonicsAPP.fla?dl=0

                   

                  Ill delete once you have it, thank you so much for this - you're a trooper !

                   

                  Jasmine

                  • 6. Re: Creating an action on a button in action script 3 adobe flash CS6
                    JoãoCésar Level 3

                    Hi. I hadn't seen your FLA while I was doing this little sample: https://goo.gl/WYWFgF

                     

                    I hope it can help you.

                     

                    Never thought it would be so challenging to code using scenes.

                     

                    Anyway... Try avoiding scenes when using AS3. It's better to rely on different frames and/or Movie Clips.

                    • 7. Re: Creating an action on a button in action script 3 adobe flash CS6
                      Colin Holgate MVP & Adobe Community Professional

                      You can delete your FLA if you like.

                       

                      The script you did was still not like my one, and you had put it into scene 2 instead of scene 1. You've also named your FLVs with the names I was suggesting for the buttons. I don't know that the videos need names, but you could name them as "sun vid", and name the buttons as "sun", etc.

                       

                      It could be worth thinking about making a single FLV that has the whole animation in it, instead of having 9 FLVs on top of each other.

                       

                      Anyway, if you rename the scenes like I show in the screenshot, rename the FLVs as "sun vid",  etc, name the buttons as "sun", "mercury", and so on. Then the script I gave would work.

                       

                      JoãoCésar's approach is ok, but might not be right for a menu that has 9 videos playing in it. Scenes in general are useful and easy to use, and I'm not sure why some people are against using them.

                       

                      Here's the Scenes menu after I had renamed them all:

                       

                      Screen Shot 2017-11-10 at 8.13.55 AM.png

                       

                      and the script that would be in frame 1 of the menu scene:

                       

                      stage.addEventListener(MouseEvent.CLICK,checkplanet);

                       

                      function checkplanet(e:MouseEvent){

                          if(e.target.name==null || e.target.name=="") return;

                           stage.removeEventListener(MouseEvent.CLICK,checkplanet);

                          gotoAndPlay(1, e.target.name+" scene");

                      }

                      • 8. Re: Creating an action on a button in action script 3 adobe flash CS6
                        jasmineeeden Level 1

                        Hi Colin :-/

                         

                        Im so sorry about this.....

                         

                        I have renamed the scenes exactly to what you did. I renamed each vid file to sun vid, moon vid etc and renamed the buttons to just Sun, Moon etc. I added a separate layer in the Menu scene called it actions and inserted the code into frame 1 - this code

                         

                        stage.addEventListener(MouseEvent.CLICK,checkplanet);

                         

                         

                         

                         

                         

                        function checkplanet(e:MouseEvent){

                         

                         

                            if(e.target.sun==null || e.target.sun=="") return;

                         

                         

                             stage.removeEventListener(MouseEvent.CLICK,checkplanet);

                         

                         

                            gotoAndPlay(1, e.target.sun+" scene2");

                         

                         

                        }

                         

                         

                        I also tried this:

                         

                         

                         

                        stage.addEventListener(MouseEvent.CLICK,checkplanet);

                         

                         

                         

                         

                         

                        function checkplanet(e:MouseEvent){

                         

                         

                            if(e.target.name==null || e.target.name=="") return;

                         

                         

                             stage.removeEventListener(MouseEvent.CLICK,checkplanet);

                         

                         

                            gotoAndPlay(1, e.target.sun+" scene2");

                         

                         

                        }

                         

                         

                        but its still not working - I am aware it is my lack of understanding :-/

                         

                        I am sorry if this is a cheeky question, but do you work for adobe or are you just a forum member ? and would you be able to link each planet to a scene for me and send it back to me so I can carry on... I will of course pay you through Paypal or some other method if you haven't Paypal.

                         

                        This project is for my MA Music dissertation and Im unable to move forward with it until i get passed this hurdle.

                         

                        If your able to help - how much would you charge ? It is clear Im at my limits with understanding flash... it would of been nice to learn flash, but its just a means to an end at the moment.

                         

                        Oh, i also forgot to say there are some codes in some of the layers because they loop.

                         

                        Thanks Colin

                        Jasmine

                        • 9. Re: Creating an action on a button in action script 3 adobe flash CS6
                          jasmineeeden Level 1

                          Hi JoaoCesar,

                           

                          thanks for the input, I have a 600frame fla because i need it to loop, so I can't click to a further blank frame outside of that. But thats a good trick for next time ! thanks !!

                           

                          Jasmine

                          • 10. Re: Creating an action on a button in action script 3 adobe flash CS6
                            JoãoCésar Level 3

                            You're welcome!

                             

                            As I said before, I did my example relying only on your first comment. If I had seen your FLA before doing the sample, surely I would have come out with a different solution for your specifc case.

                             

                            Please let me know if Colin can't help you.

                            • 11. Re: Creating an action on a button in action script 3 adobe flash CS6
                              jasmineeeden Level 1

                              Colin - I have a sneaky feeling that it might have something to do with how i composed each button - as I don't know what I'm doing, when I converted the shape to an invisible symbol on the Up keyframe, I then pressed the Over put a keyframe in and the only way i could get the filters tab up (because i wanted it like a glow) was to convert it to a movie clip - so had to convert to symbol again and then again on the down key.

                               

                              I think I might start again to be honest if you can't help

                              Thanks

                              Jasmine

                              • 12. Re: Creating an action on a button in action script 3 adobe flash CS6
                                jasmineeeden Level 1

                                EEEEEP ! COLIN !!

                                 

                                I started from scratch - tested it with just one planet in the new composition and it works ! I think the problems where because I had multiple symbols inside 1 button so it didn't know what to listen too! FEW !

                                 

                                Thanks so much for your help

                                 

                                you are a true gent

                                Jasmine

                                • 13. Re: Creating an action on a button in action script 3 adobe flash CS6
                                  jasmineeeden Level 1

                                  yeah - its as soon as i put the looping action code in - the button function stops working

                                   

                                   

                                   

                                  anyway, I'm gonna redo the videos for a longer period of time instead of looping it - that will solve that problem

                                  Jasmine

                                  • 14. Re: Creating an action on a button in action script 3 adobe flash CS6
                                    Colin Holgate MVP & Adobe Community Professional

                                    Glad you're getting it sorted out. When you are doing this line:

                                     

                                    gotoAndPlay(1, e.target.sun+" scene2");

                                     

                                    that is still the wrong way, it should be:

                                     

                                    gotoAndPlay(1, e.target.name+" scene");

                                     

                                    Also, if you have named the button as "sun" and the scene as "Sun scene", or "sun Scene", it may well fail. The e.target.name+" scene" part is creating a scene name from the button and, a space character, and the word "scene". If you clicked on the sun button, the scene name would come out as "sun scene". If you clicked on the mars button, the same code would give you a scene name of "mars scene".

                                    • 15. Re: Creating an action on a button in action script 3 adobe flash CS6
                                      jasmineeeden Level 1

                                      Ok ill give it a go thanks Colin

                                       

                                      Jasmine