33 Replies Latest reply: Jan 24, 2012 5:12 AM by dmeN RSS

    Adding action script to instances...

    Bloom
      I'm pretty frustrated and am done searching the site. Awful...just awful. Anyway...

      Using Flash CS3 for the first time since upgrading. I'm not able to add action script to instances. Certainly this isn't a change. I've created a button symbol. I've named the instance upon dragging it on the stage. Now it tells me I can't apply actions to it.

      I hope I'm just missing a step.
        • 1. Re: Adding action script to instances...
          Andrei1 Community Member
          How are you applying actions?
          • 2. Re: Adding action script to instances...
            abeall Community Member
            You're not missing anything. In AS3 with CS3 and CS4 this feature has been removed from Flash. Yes, it sucks.

            If you want to continue using AS3, you will need to give the button an instance name, and assign an action on the timeline which will look something like this:

            myButton.addEventListener('click',myButtonClick);
            function myButtonClick(event){
            // do stuff
            }

            Frankly, you probably want to just use AS2. You can set this in the publish settings or when creating a new document, choose the option which says "Flash File (ActionScript 2.0)"
            • 3. Re: Adding action script to instances...
              Bloom
              Is there a reason why they went away from the VERY EASY method of applying actions to instances?
              • 4. Re: Adding action script to instances...
                Andrei1 Community Member
                Bloom, are you talking about writing actions INSIDE the symbols? If so - they went with a more robust approach of writing classes for the symbols. It takes not such a big effort to change the ways. In essence it is the same thing but gives one much bigger flexibility.

                Also, are you familiar with AS3 event model?
                • 5. Re: Adding action script to instances...
                  Bloom
                  Andrei, I'm willing to learn, but most of my use is recreational so using 2.0 isn't a big deal. (Of course I'm having troubles getting it to work too.) It's just been awhile.

                  I am not familiar with the AS3 event model. I am talking about clicking on an Instance and being able to apply actions to it.



                  Meanwhile, I'm trying to apply the following using AS2:

                  on (rollOver) {stageRoster.loadMovie("playerMovies/hahnJordan09.swf");
                  }

                  This doesn't appear to be working AS2. I have my stage instance labeled, and I have my external SWF file uploaded.
                  • 6. Re: Adding action script to instances...
                    abeall Community Member
                    Andrei1, Bloom is not referring to class/symbol-definition code, but instance code. There is no IDE solution as of CS3 and CS4 for AS3.

                    Bloom, are you sure the path is correct? Note that the path "playerMovies/hahnJordan09.swf" is relative to the embedding HTML page, not the swf.
                    • 7. Re: Adding action script to instances...
                      Bloom
                      I made the change in the path, but it's still not working. The code now reads:

                      on (rollOver) {stageRoster.loadMovie("roster/2011/playerMovies/hahnJordan09.swf");
                      }


                      Here is the PHP page in question:

                      http://spieceselect.org/roster/2011/index2.php

                      Click on Jordan Hahn's name. It's the only one I've done so far.

                      • 8. Re: Adding action script to instances...
                        Newsgroup_User Community Member
                        I made the change in the path, but it's still not working. The code now reads:

                        on (rollOver)
                        {stageRoster.loadMovie("roster/2011/playerMovies/hahnJordan09.swf");
                        }


                        Here is the PHP page in question:

                        http://spieceselect.org/roster/2011/index2.php

                        Click on Jordan Hahn's name. It's the only one I've done so far.



                        • 9. Re: Adding action script to instances...
                          Newsgroup_User Community Member
                          As abeall said you need to indicate to CS3 that you use AS2

                          When you refer to an instance in AS2 on the time line where the instance is
                          you code should be:

                          myButton.onRollOver = function()
                          {stageRoster.loadMovie("playerMovies/hahnJordan09.swf");}



                          • 10. Adding action script to instances...
                            Andrei1 Community Member
                            As abeall said you need to indicate to CS3 that you use AS2

                            When you refer to an instance in AS2 on the time line where the instance is your code should be:

                            myButton.onRollOver = function() {stageRoster.loadMovie("playerMovies/hahnJordan09.swf");}

                            Your original code should work from withing the instance in AS2. But you will have to change reference to stageRoaster if it is located on the main timeline.

                            • 11. Re: Adding action script to instances...
                              abeall Community Member
                              Bloom,

                              I think you had it right the first time, I just didn't know where you had your files and it's a common misunderstanding. But the correct relative url should indeed be "playerMovies/hahnJordan09.swf"

                              If you want to try an absolute url you can use "/roster/2011/playerMovies/hahnJordan09.swf"

                              Are you sure stageRoster is correct? What happens if you do this:
                              on (rollOver){
                              trace(stageRoster);
                              stageRoster.loadMovie("playerMovies/hahnJordan09.swf");
                              }

                              Andrei1, MovieClip.onRollOver is not the syntax Bloom is using, in AS2 you can attach code directly to symbol instances using the on() and onClipEvent() syntax (not putting the code in the timeline.)
                              • 12. Re: Adding action script to instances...
                                Bloom
                                So if I do what Andrei says, it should be using AS3, correct?

                                Right now I'm set up to use AS2.


                                Abeall,

                                Right now I have 2009.swf on the linked page. It's in the folder roster/2011. The hahnJordan09.swf is located at roster/2011/playerMovies.

                                I had already changed my path to the absolute /roster/2011/playerMovies/hahnJordan09.swf.

                                stageRoster is the name of the symbol AND instance. I changed the name of the instance to stage_Roster (in the script too), but none of it worked.

                                • 13. Re: Adding action script to instances...
                                  Bloom
                                  Right now my code is place within the Instance.

                                  on (rollOver) {loadMovie("/roster/2011/playerMovies/hahnJordan09.swf",stageRoster);
                                  }
                                  • 14. Re: Adding action script to instances...
                                    abeall Community Member
                                    Did you try the trace command? This will put something in your Output window when you test in Flash, telling you if the reference stageRoster/stage_Roster is correct.
                                    • 15. Re: Adding action script to instances...
                                      Bloom
                                      I just did. No change. It just does nothing.
                                      • 16. Re: Adding action script to instances...
                                        abeall Community Member
                                        If it does nothing at all then your code is not attached at the time you are rolling over the button. How many keyframes do you have for the button?
                                        • 17. Re: Adding action script to instances...
                                          Andrei1 Community Member
                                          quote:

                                          Originally posted by: Bloom
                                          So if I do what Andrei says, it should be using AS3, correct?




                                          No, what I suggested was AS2 code. This code is used when onRollOver functions is assigned to your button on the main timeline.

                                          Since you are using the on(rollover) from withing the instance and if stageRoaster is located on main timeline - you need to change the scope of stageRoaster. It either can be _root or _parent:

                                          on (rollOver) {_root.stageRoster.loadMovie("playerMovies/hahnJordan09.swf");
                                          }

                                          • 18. Re: Adding action script to instances...
                                            Bloom
                                            quote:

                                            Originally posted by: abeall
                                            If it does nothing at all then your code is not attached at the time you are rolling over the button. How many keyframes do you have for the button?


                                            Aaron, not sure what it means to not be attached. Right now I just have one keyframe with ten layers. One layer for my stage (stage_Roster) and nine layers for the players. I put the code in the Actions panel within the Instance.
                                            • 19. Re: Adding action script to instances...
                                              Bloom
                                              quote:

                                              Originally posted by: Andrei1
                                              quote:

                                              Originally posted by: Bloom
                                              So if I do what Andrei says, it should be using AS3, correct?




                                              No, what I suggested was AS2 code. This code is used when onRollOver functions is assigned to your button on the main timeline.

                                              Since you are using the on(rollover) from withing the instance and if stageRoaster is located on main timeline - you need to change the scope of stageRoaster. It either can be _root or _parent:

                                              on (rollOver) {_root.stageRoster.loadMovie("playerMovies/hahnJordan09.swf");
                                              }




                                              The _root addition didn't solve the problem, but it made sense to me. : )

                                              • 20. Adding action script to instances...
                                                Andrei1 Community Member
                                                I was getting ready for dinner and Roster become Roaster. Hmmm... :-)

                                                Anyway, where is the stageRoster (or Roaster or Grill) relative to the button?
                                                • 21. Re: Adding action script to instances...
                                                  Bloom
                                                  stageRoster and button_jh are on main timeline.

                                                  • 22. Re: Adding action script to instances...
                                                    Bloom
                                                    It's frustrating still. I'm doing it exactly as I have seen in other resources, including a couple of other sample here. I have everything set up properly, in terms of using AS2. I have files uploaded. I have tried various different pathways.
                                                    • 23. Re: Adding action script to instances...
                                                      Bloom
                                                      I'm using a Loader component now, naming the instance stage_Roster. It's now working.

                                                      Loathsome process now. : (

                                                      I assume there is an upside to these changes forcing this learning curve.

                                                      • 24. Re: Adding action script to instances...
                                                        Andrei1 Community Member
                                                        Can you try:

                                                        on (rollOver) {
                                                        trace("_root.stageRoster: " + _root.stageRoster);
                                                        trace("_parent.stageRoster: " + _parent.stageRoster);
                                                        }

                                                        What is the output?

                                                        • 25. Re: Adding action script to instances...
                                                          Bloom
                                                          It's working using the Loader component. I assume I would use that with AS3 too. How would I go about getting the desired results using AS3?

                                                          • 26. Adding action script to instances...
                                                            Andrei1 Community Member
                                                            abeall wrote the code that you need to attach to the button on the main time line. As said before you cannot have a code inside the symbol. Writing class will not make sense because things are happening on the main time line and the only thing you need to make the button do something.

                                                            As for loading a swf - you can use Loader() class and load the swf into a container of your choosing.

                                                            I really suggest you get familiar with AS3 event model. Believe me - without understanding it you will be lost for quite awhile. Event model is a center piece of AS3 and one of the most powerful features of the language. Once you understand it - a lot of tasks will be easy.

                                                            Another thing you need to know is AS3 display list model.


                                                            • 27. Re: Adding action script to instances...
                                                              Bloom
                                                              Loader() class? I thought using AS2, I could just create a stage, give it an instance name, and load into that. That didn't work. Is Loader() new to CS3?

                                                              Let me ask this about what Abeall wrote:

                                                              I have 9 buttons on the stage right now. Would I write the code for button_jh in the frame of the layer that button is in, or do I just write it in a code layer within that frame?

                                                              This stuff does intrigue me.
                                                              • 28. Re: Adding action script to instances...
                                                                Bloom
                                                                Display list model?
                                                                • 29. Re: Adding action script to instances...
                                                                  abeall Community Member
                                                                  Buttons are unique in that they resolve relative paths to their parent timeline. In other words stageRoster.loadeMovie is the same as if you had a MovieClip and you had _parent.stageMovie.loadMovie. You shouldn't need to use a Loader component but if it works then go for it.

                                                                  Concerning your 9 buttons, in AS3 yes, you would put the code on the buttons' parent timeline at the frame the buttons show up, referencing each button by unique name.

                                                                  In AS3 it's basically the same as AS2 except most of the automatic stuff that loadMovie does for you is removed. loadMovie will load the external assets and you see it immediately when it loads at the target you loaded it to. In AS3 the Loader class (different than the Loader component in AS2) is used to load something, then you take an additional step to add it to a timeline (display list) somewhere.
                                                                  • 30. Re: Adding action script to instances...
                                                                    Andrei1 Community Member
                                                                    In addition to Aaron's reply.

                                                                    quote:

                                                                    Would I write the code for button_jh in the frame of the layer that button is in, or do I just write it in a code layer within that frame?


                                                                    In principle it depends on your preferences. It is advisable, though, for manageability sake to write code in layers that are devoted to script only.

                                                                    quote:

                                                                    I thought using AS2, I could just create a stage, give it an instance name, and load into that.


                                                                    I understand that by "stage" you mean a container for you visual elements. Term "stage" has a special meaning in Flash.

                                                                    quote:

                                                                    Is Loader() new to CS3?


                                                                    Yes, it is. There are several features in this class. This class designed to load files that you will be able to manipulate or communicate with in addition to that you will be able to display them. These files are SWFs or images (PNG, GIF, JPEG).

                                                                    quote:

                                                                    Display list model?


                                                                    Yes, display list is a very important thing to understand. In short it is how visual objects are positioned relative to each other. Highlights are:

                                                                    Objects are added via addChild, addChildAt methods. There are many methods that allow you to read objects' positions.

                                                                    Depth is changed now. There is no negative depths and all the objects are placed in 0-based order WITHOUT gaps.
                                                                    • 31. Re: Adding action script to instances...
                                                                      Bloom
                                                                      Stage, container. Yep. : )

                                                                      Back to the buttons. So what I'm doing now in AS3 is actually establishing the buttons via code beyond just giving them an instance name. It's seems like I'm naming the Instance then coding in the notion that I'm going to use the button, then actually defining what the button does. Correct?

                                                                      • 32. Re: Adding action script to instances...
                                                                        IgnatzP

                                                                        Bloom, 3 years later and I'm using CS4 trying to figure out the same thing. In fact, I'm trying to find clear explanations of how action scripts work, how to attach them (my efforts keep not working and also turning off behaviors I've attached. Did you find an answer and can you point me towards a source? Adobe seems to have left the building.

                                                                        • 33. Re: Adding action script to instances...
                                                                          dmeN Community Member

                                                                          >>Yes, it sucks.

                                                                           

                                                                          No, it doesn't. It was poor style to ever do that and good developers didn't anyway. Now, you're forced to produce cleaner code, as it's more consolidated, and not scattered over a bunch of objects.