Making Buttons Work in Flash CS3 (A Reply)

ActionScript 3.0 Flash

Often, but not always, I syndicate my blog entries on the blog at Community MX, CMXtraneous.  That particular blog has a word limit on article length, so not everything I write makes it over — that, and sometimes I simply forget to copy/paste my content to both locations.  In any case, I don’t see replies to the CMX entries nearly as often as I do my own, but I do try to make the rounds.  I just found three replies to my recent “Making Buttons Work in Flash CS3 (ActionScript 3.0),” and since the CMX interface doesn’t seem to like replies with sample code in them, I’m replying to those comments here. 

Anon

Doesn’t work.

I copied and pasted directly from the article, and I have to say … it works just fine for me.  :)   Can you be more specific about what doesn’t work?  Are you using the AS3 code in an AS3-based FLA file?

The MalloMan

This creates the trace output correctly, however, I have not been able to understand how to get a button to navigate to another Web page.

For that, you’ll need the navigateToURL() function, which replaces the older getURL().  The URL you provide (as a parameter) needs to be an instance of the URLRequest class, so your final code might look like this:

myButton.addEventListener(
  MouseEvent.MOUSE_UP,
  function(evt:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.domain.com/"));
  }
);

Do you know how to take this code that you’ve written here and actually make the button work other than create an output in your output manager? Any help would be greatly appreciated, thanks!

Sure thing.  Let’s look at wes’s feedback and take it from there.

wes

I’m tired of seeing example for buttons in AS3 where all you are doing is tracing out ("You clicked me").

Fair enough.

I’m sorry, but that is not at all how people will be building buttons

It’s great for a proof of concept!

… and the way to customized each button’s behavior in AS3 is much more involved.

Well, that’s just it.  The “more involved” part is spelled out in the original blog entry.  What you put inside the event handler function is up to you.  I used a trace() statement because that was a generic approach.  If you wanted to affect another movie clip, for example, you would have to give that other movie clip an instance name, then refer to that instance name and invoke MovieClip class members on the instance.

myButton.addEventListener(
  MouseEvent.MOUSE_UP,
  function(evt:MouseEvent):void {
    someOtherClip.gotoAndPlay(15);
  }
);

In the above snippet, a movie clip with the instance name someOtherClip is told to perform the MovieClip.gotoAndPlay() method, which means when the myButton instance experiences a “mouse up” event, someOtherClip will go to frame 15 of its own timeline and play from there.

If you had already prepared an instance of the Sound class and loaded an audio file into it, you could cause that instance to play in much the same manner:

myButton.addEventListener(
  MouseEvent.MOUSE_UP,
  function(evt:MouseEvent):void {
    aSoundInstance.play();
  }
);

How about discussing eventDispatcher and making custom events that work?

Custom events are a good topic, but they’re more advanced than the concept I had envisioned for this particular blog entry.  This one was just a quick way to answer the question, “Why don’t buttons work any more in Flash CS3?” which I had been seeing in the Adobe forums when I wrote the article.

Conceptually, AS3 still operates in the same object-oriented manner as AS2.  It’s just that the event handling mechanism as a whole now behaves similarly to the event handling technique used with AS2’s v2 Components, which also use an addEventListener() method.

I hope this clears things up a bit.  :)

Leave a Reply