1 Reply Latest reply on Oct 20, 2006 5:13 AM by TimSymons

    access Button contained in a movie clip

    at_play Level 1
      This is for beginners or those not familiar or having diffculty with getting embedded buttons to work.

      This is a how to on using actionscript in your main time line that listens for events for button(s) located inside of a movieclip.

      I spent hours trying to figure out why I could not access my button that was inside of a movie clip from the main ROOT or _level0 of my scene. When I placed the AS inside the movieclip that the button was inside of, the button worked but when I put the same code, no changes, on the main timeline nothing happened.

      myMovie_mc.myButton_btn.onRelease = function() {
      trace("The Button Works!");

      Your can also use onPress, onRollOver or any of the button actions you would if you created the AS on the button itself.

      myMovie_mc.myButton_btn.onRelease = function() {

      function() part of the code can also be function():Void

      First you tell flash where the button is located. Starting from the main timeline you start with the name of the movie. This is where I had trouble and why my buttons did not work. This part of the code is the INSTANCE name NOT the label you named the movie that displays in the library.

      CREATE AN INSTANCE NAME on the movie clip. Select the movie, in the properties bar you will see a field where you place the instance name. I always end the name with _mc, or _btn to tell me that it is a movie or a button so when I go back through the AS I know what that object is that is being referenced.

      myMovie_mc <-- This is the INSTANCE NAME of the movie clip

      myButton_btn <-- This is the name of the button INSTANCE NAME that resides inside the myMovie_mc movie clip

      onRelease <-- This is the event listened for, it could also be onPress, onRollOver or any other button events

      = function() <-- This is needed and tells flash that this code is treated as a function so that when the event happens, in this case onRelease, the code inside the { } is executed. This could also be = function():Void

      All the parts before the set to symbol or the = sign needs to be seperated with a period, though NOT between the event listened for and the set to symbol or = sign.

      myMovie_mc.myButton_btn.onRelease = function():Void
      Put your code here that you want performed when button is Released...

      The reason my code didn't work was because I did not give an instance name to the movie clip. You must give instance names to ANYTHING you call within AS code. Including buttons, MC's, symbols etc... or else nothing will happen.

      Now, if you are having same problem I had, the AS worked on the same time line level as the button was on but NOT when placed in parent or main time line, you now know how to fix it.

      This works in AS2, Flash8 and Flash9. I don't have AS3 yet but the code should work in AS3 also.

      Hope this helps someone, I searched for this info in the forums and could NOT find it. I stumbled upon my error after many hours of trial and error. So next time if I have this problem again, a forum search will bring this post up in the results. LOL


        • 1. Re: access Button contained in a movie clip
          TimSymons Level 1
          Nice post. One small addition to this is that you can also just specify a function name instead of using "=function() {".

          For example:

          myMovie_mc.myButton_btn.onRelease = myFunction;

          function myFunction():Void
          Put your code here that you want performed when button is Released...

          This could be used in a couple of ways. First, the way you mentioned in your post. Second, you can use this method to have the program simulate a button press (even if the user does not click on the button). Since the function will execute even if you just call it like: myFunction();

          Just my 2 cents.