Using JSFL to create auto-buttons in Flash

February 1st, 2008 by Steven Sacks

Building upon the previous post, one of the other things I do quite often is create MovieClip buttons using a little-known trick in Flash.

You create a MovieClip, and in the MovieClip, you make three keyframes, name them "_up", "_over", and "_down" (_down is optional) and then all you have to do is assign any mouse action to the MovieClip such as "onRollOver" or "onRelease" and it will automatically behave like a Button symbol, no code required. The benefit of this is you can target scopes in and out of the MovieClip, whereas with a Button symbol, you cannot.

So, I wrote this JSFL script that makes it easy for me, and assigned it to SHIFT+F8 (who really needs a shortcut to open the project panel, anyway?). What it does is creates a new MovieClip symbol, adds a second keyframe and two layers. It creates the "_up", "_over" and "_down" frames, and puts a stop action at the top (required). It also names the instance after the symbol.

if (fl.getDocumentDOM().selection.length > 0)
{
        var mcName = prompt("BUTTON Name", "");
        if (mcName != null)
        {
                var newMc = fl.getDocumentDOM().convertToSymbol("movie clip", mcName, "top left");
                fl.getDocumentDOM().selection[0].name = mcName;
                fl.getDocumentDOM().enterEditMode("inPlace");
                var tl = fl.getDocumentDOM().getTimeline();
                tl.insertFrames(2);
                tl.convertToKeyframes(0, 2);
                tl.addNewLayer("LABELS");
                tl.convertToKeyframes(0, 2);
                tl.layers[tl.currentLayer].frames[0].name = "_up";
                tl.layers[tl.currentLayer].frames[1].name = "_over";
                tl.layers[tl.currentLayer].frames[2].name = "_down";
                tl.addNewLayer("AS");
                tl.layers[tl.currentLayer].frames[0].actionScript = "stop();";
                fl.getDocumentDOM().exitEditMode();
        }
}

Posted in Flash, JSFL, Tips/Tricks

6 Responses

  1. Jorge Rego

    Hi Steven,

    Great job! I'm a flash newbie, but im getting to enjoy it, A LOT… and these .jsfl stuff really rocks.

    In your code above i switched the line tl.convertToKeyframes(0, 2); to tl.convertToKeyframes(0, 3); because it wasn't creating the "_over" keyframe.

    By now i know that we can access and edit the timeline, the frames, keyframes, etc… do you think it's possible to create one command that "converts" a previously 12 fps animation movie, to 24 fps?

    Jorge

  2. Steven Sacks

    If you want to learn what the JSFL commands for actions in Flash are, open the History window and look at the source JSFL of any action you take, like changing the FPS of the movie.

  3. Cor

    Hi Steven,

    I am VERY new at this but trying to learn.
    I am trying your code but I guess I have to do some additional things to make it work. but what? Or can you give me an direction where I can find some basics about using JSFL? Thanks!
    Cor

  4. { aut0poietic } » AutoButton JSFL

    [...] behave like buttons by adding the 3 frames, and labeling them "_up", "_over" and "_down" and made this cool JSFL script to do that for him automatically. I finally got around to trying out his script, and I needed a few things done differently. Here's [...]

  5. Justin_P

    Nice work, Steven!
    I added some space to the frames, locked the actions & labels layers, and set button mode to TRUE to trigger the button behaviors.

    if (fl.getDocumentDOM().selection.length > 0)
    {
    var mcName = prompt("BUTTON Name", "");
    if (mcName != null)
    {
    var newMc = fl.getDocumentDOM().convertToSymbol("movie clip", mcName, "top left");
    fl.getDocumentDOM().selection[0].name = mcName;
    fl.getDocumentDOM().enterEditMode("inPlace");
    var tl = fl.getDocumentDOM().getTimeline();
    tl.insertFrames(29);
    tl.addNewLayer("labels");
    tl.layers[tl.currentLayer].frames[0].name = "_up";
    tl.insertKeyframe(9);
    tl.layers[tl.currentLayer].frames[9].name = "_over";
    tl.insertKeyframe(19);
    tl.layers[tl.currentLayer].frames[19].name = "_down";
    tl.layers[tl.currentLayer].locked = true;
    tl.addNewLayer("actions");
    tl.layers[tl.currentLayer].frames[0].actionScript = "stop(); \r this.buttonMode = true;";
    tl.layers[tl.currentLayer].locked = true;
    fl.getDocumentDOM().exitEditMode();
    }
    }

  6. michael

    Or,

    you could make a movie clip, and then on its parent stage click, choose button and track as button.

    It will be a movieclip where frame 1 is up, 2 is over, 3 is down and 4 is your hit. I last did this today.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Steven Sacks

I am a professional Flash developer with over 13 years of programming experience. I have consulted for high-profile agencies and companies in San Francisco, Los Angeles, Atlanta and New York, and developed numerous award-winning websites and rich internet applications for clients including Adobe, Fox Sports, FX Networks, Anheuser-Busch, GE, DirecTV, ESPN, The Weather Channel, Home Depot, and Coca-Cola.

I am the author of the open-source Gaia Framework for Adobe Flash, which dramatically reduces development time and makes developing Flash sites much easier.