To dynamically create a button (clickable MovieClip) with ActionScript you only have to define the onRelease event. For example this makes a button out of your MovieClip ‘MyMovieClip’.

class com.mywebsite.MyMovieClip extends MovieClip
{

function MyMovieClip()
{}
public function onRelease()
{
//Do Something
}

}

Frustrating thing however is that your MovieClip is now only clickable on its graphics. So say it has some text and some lines, it will be only clickable when the mouse is exactly hovering above character-pixels or precisely above the line-pixels.

I found a real easy trick that makes a user defined region clickable. Dynamically draw a invisible shape(!) defining the region you want to be clickable using the Flash drawing API. This invisible drawing is impossible in the authoring tool. I tried it, but invisible graphics collapse/disappear.

I defined a function handling the onLoadInit event to draw my “click”-region.

public function onLoadInit()
{

drawClickableRegion();

}

private function drawClickableRegion()
{

beginFill(0xFFFFFF, 0);
moveTo(-2.5, -5);
lineStyle(0, 0xFFFFFF, 0);
lineTo(-2.5,5);
lineTo(35,5);
lineTo(35,-5);
lineTo(-2.5,-5);
endFill();

}

As you can see, my code draws a box between -2.5,-5 and 35,5. I use fill- and line-color 0xFFFFFF with an alpha 0. You can use any arbitrarily chosen color but make sure you set the alpha to 0 to make your shape invisible. From now onwards your dynamically created MovieClip is a button. Remember to redraw this region whenever you use the clear() function on this MovieClip elsewhere in your code.

2 Responses to “Dynamically create a Flash button from a MovieClip”

  1. Per Försund Says:

    Hi

    I have tried your code -Dynamically create a Flash button from a MovieClip- and I can´t get it to work.

    The need is to click one of the 23 movie clips in the movie, and let one and each of them gotoAndPlay another frame. Easy? Not for me: new to this -reading and learning- but since this makes my time shorter -and are important- I searched the web and found your blog as the best result.

    Can you explain what code I need and where to put it? If you do so, I am very thankful to you. You then lets me finish an example of design I have promised, since I am a graphic designer.

    Regards
    Per Försund
    Reklambyran.se


  2. That does indeed not sound really complicated.

    What version of Flash and ActionScript are you working with?

    I would

    First design the 23 MovieClips
    Convert them in to buttons.

    Right Click on them
    Choose “Convert to symbol…”
    Choose “Button”

    Place actionscript on the buttons
    onRelease = function()
    {
    gotoAndPlay(frame);
    };

    I hope this helps!
    Good luck.


Leave a Reply