Using JSFL to create auto-buttons in Flash
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
March 5th, 2008 at 2:55 am
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
March 5th, 2008 at 10:56 am
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.
March 30th, 2008 at 1:34 am
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
June 12th, 2008 at 9:06 am
[...] 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 [...]
October 20th, 2008 at 1:30 pm
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();
}
}
January 29th, 2009 at 6:30 pm
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.