Search code examples
actionscriptscopeactionscript-2

Scope issue - Controlling a movieclip inside a button with actionscript


I'm trying to show/hide a movieclip (or graphic) symbol that is on a layer of a button symbol using actionscript 2. Here's what I tried

in the actions for the button:

on (release) {
 this.button_name.movieclip_name._alpha = 0;
 trace(this.button_name.movieclip_name);
}

and the trace returns undefined... so I think I've got a problem understanding how to address the child element. However I am not a flash programmer... just hacking on it at the moment for a side project, so I probably just don't understand how it works.

Thanks, Jim :)


Solution

  • For AS2, it's not a good idea to put MovieClips inside buttons. The easiest and most direct approach is to restructure things so that your button and the movieclip you had inside it are at the same level, perhaps within a new MC created to contain them. You should think of the Button symbol as a thing that only provides a clickable hit state, but is not a container for other things.

    As for your followup, yep, you can indeed use MovieClips as buttons. If you give your MC functions to handle button-like events (onPress, onRelease, onReleaseOutside and so on), those functions will get called just like on a Button. You can also control the finer details - see the docs on MovieClip.hitArea and MovieClip.useHandCursor.

    One thing I've done frequently is to create frames in the MC called "show" and "hide", followed by short animations and a "stop()" command, and then done something like this:

    myMC.onRollOver = function() { gotoAndPlay("show"); }
    myMC.onRollOut = myMC.onReleaseOutside = function() { gotoAndPlay("hide"); }
    myMC.onRelease = function() {
        // do something....
    }