Groups | Blog | Home
all groups > flash actionscript > december 2004 >

flash actionscript : Adding Actionscript to a button, not an instance



Antman1313
12/22/2004 9:16:40 PM
Greetings all: Old Director guy here, making the transition to Flash, slowly
but surely. Forgive the newbie question, but... Is there any way to hang
actionscript off of the buttons themselves? Basically, I've got a ton of
buttons that will appear in several places across the project, but they will
always do the same action (playing FLV files, in this case). Anyway, I'd like
to write one piece of code ONCE and hang it off the button itself. That way,
every time I pop an instance on the stage, the code's already there, attached
to the button (not the instance). Am I making sense? Hard to tell these days.
Anyway, is there a way to do this? Or, failing that, is there another way to
skin this cat? Thanks in advance! --Ant. P.S. No cats were harmed in the
creation of this post.
juankpro
12/22/2004 9:51:12 PM
In macromedia MX you could do the following. Create a button, the create an
empty MovieClip, darg your button inside and write the code you need. The next
time you need the Button you have to use the movieClip with the button inside
instead of the button itself.
juankpro
12/22/2004 10:01:11 PM
In Flash MX 2004 there is an elegant way to do the same.
1. Create an ActionScript .as file and name it for example MyButton
2. Then write the following code:

class MyButton extends MovieClip{
function onRelease(){
// Put your code here
}
}

3. Create a flash movie and create a MovieClip to act as the button.
4. Right click the new MovieClip in the library and select Linkage...
5. In the linkage properties dialog select Export for ActionScript
6. In the AS 2.0 Class TextField type: "MyButton" and click OK

The button is ready, just remember to save boths files .as and .fla in the
same folder.
juankpro
12/22/2004 10:18:44 PM
In flash MX you can use the same Object-Oriented approach doing this:

1. Create a MovieClip to act as the button.
2. Inside the MovieClip put the following code on a new layer inside the
movieclip you created:

#initclip 0
function myButton(){}
myButton.prototype = new MovieClip();
myButton.prototype.onRelease = function(){
// Put your code here
};
Object.registerClass("myButtonSymbol", myButton);
#endinitclip

3. Right click the movieclip in the symbol and select Linkage
4. Select Export for ActionScript
5. in the linkage properties write myButtonSymbol on the identifier Textfield
and click OK

Ready
Antman1313
12/23/2004 2:31:30 AM
Hey Juan: Thanks for the reply. I'll probably use the MC method. At this stage
of development, it shouldn't set me too far back. The O-O method worked
swimmingly, BTW, but I'd have to backtrack too much to use it. I'm definitely
noting it for future use, though. Thanks again! --Ant.
AddThis Social Bookmark Button