7 Replies Latest reply on Oct 6, 2016 4:29 PM by JoaoCesar

    Call methods and properties created onto a MovieClip

    JoaoCesar Level 1

      Hi people!

       

      I've noticed that Animate CC now allows us to add AS3 onto MovieClips on stage, like in the AS2 era.

       

      So how do I correctly declare functions and variables onto a MovieClip and call them from the main timeline?

       

       

      Thanks!

        • 1. Re: Call methods and properties created onto a MovieClip
          Colin Holgate MVP & Adobe Community Professional

          If you look carefully, the script you are adding to a movieclip is really going into the main timeline, at the same level as the movieclip. It's an illusion that it's going onto the movieclip itself. Try this script, having selected a movieclip with an instance name of say "box":

           

          import flash.events.MouseEvent;

           

          var something:* = this

          this.addEventListener(MouseEvent.CLICK,clicked);

          function clicked(e:MouseEvent){

            trace(something.name);

          }

           

          It will trace 'root1', not 'box'.

           

          If you were to put scripts into the timeline of the movieclip itself, you could call them with:

           

          box.somefunction();

           

          if the instance name is box. Like this in the main timeline:

           

          import flash.events.MouseEvent;

           

          var something:* = this

          this.addEventListener(MouseEvent.CLICK,clicked);

           

          function clicked(e:MouseEvent){

            box.somefunction();

            trace(something.name);

          }

           

          and this in the movieclip named box:

           

          function somefunction(){

            trace("hi");

          }

           

          would trace 'hi' and 'root1' when you click on the box.

          • 2. Re: Call methods and properties created onto a MovieClip
            JoaoCesar Level 1

            Hey, Colin! Thank you for the answer.

             

             

            You're right. It indeed traces that "this" is referencing to the root.

             

            So this is very misleading. I'm far away from being an AS3 expert, but I think it would be way better if we could add script to objects like Unity does, for example.

            • 3. Re: Call methods and properties created onto a MovieClip
              Colin Holgate MVP & Adobe Community Professional

              Most other tools work that way too. If you put your movieclip specific function in the timeline of the movieclip itself, it's nearly as good. In some ways it's better, in that every copy of the movieclip will have an up to date script. If the script is attached to the instance, you have to have as many copies of the script as there are instances of that type of movieclip, and it's easy to get into trouble with different versions of the script happening.

              • 4. Re: Call methods and properties created onto a MovieClip
                Colin Holgate MVP & Adobe Community Professional

                By the way, with AS3 you can have external script files, and they get attached to the movieclip in the library, using the sharing feature. You do have to get to understand packages, private, public, and so on, but you can get past that difficulty soon enough.

                • 5. Re: Call methods and properties created onto a MovieClip
                  JoaoCesar Level 1

                  Very cool, Colin! Thanks again.

                   

                  I didn't know about attaching external script files to Movie Clips using this sharing feature. Is this the same as linking external .as files?

                   

                  I've already created apps and games using external .as files, but I client wants me to create an e-learning web app using Flash Player and there's a big chance I'll have to code everything inside of the FLA.

                  • 6. Re: Call methods and properties created onto a MovieClip
                    Colin Holgate MVP & Adobe Community Professional

                    There is #include externalfile.as, which effectively copies the external file into the timeline code. That's a bit different, the sharing external file way lets you get into object oriented programming, where you can import other class files, or extend other class files.

                     

                    You don't have to work that way, and could just use them as a way to have external code files. It is definitely possible to code everything in the timeline, and if you were doing AS2 before that would be more familiar. It's also the way you need to work when doing HTML5 Canvas Javascript.

                    • 7. Re: Call methods and properties created onto a MovieClip
                      JoaoCesar Level 1

                      Nice, Colin! You're great!

                       

                      I'll take a look at recent tutorials and articles about AS3 and HTML5 to get up to date and also to remember some things.

                       

                       

                      Thank you so much for you time and patience!