Flash AS3 - can you help me associate a custom class with my .fla?

Hello,

I am a complete beginner with AS3 and OOLs but have been trying to read a lot and getting some help in developing a simple flash website.  BUT!  I still cant understand / find a clear explanation of how to associate my custom button class with my .fla.

I have six movieclip buttons each slightly different and am trying to communicate with them without writing a new function for each button.

A very helpful expert pointed me in the direction of the code attached below.  However when I try to link the class to each movie clip in the library Flash tells me I have to enter... a unique class name not associated with any other symbols!!?

Does this mean I have to create a class for every button after all?
package{
        //KeyButton.as
        import flash.display.*;
        import flash.events.*;
 
        public class KeyButton extends MovieClip{
                public function KeyButton(){
                        addEventListener(MouseEvent.MOUSE_OVER,keyOver);
                        addEventListener(MouseEvent.MOUSE_OUT,keyOut);
                }
                private function keyOver(e:MouseEvent):void{
                e.currentTarget.gotoAndPlay('over');
                }
                private function keyOut(e:MouseEvent):void{
                e.currentTarget.gotoAndPlay('out');
                }
        }
}

Open in new window

LVL 1
keybordAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
julianopolitoConnect With a Mentor Commented:
OK, you can make a slight different classes for each of your symbols, BUT , since you have the SAME functionality for all 6 buttons, lets do it another way, just keeping the same and only class.

Flash really needs a single identifier for each button, but you can have a different base class for the buttons and let flash create the identifiers for you. Do this:"

-Right Click the KeyButton movieclip in the library.
-Select Linkage...
-Check Export for Actionscript
-In the Base Class field, write KeyButton (it is case sensitive)
-In the Class field put something like KeyButton1 (change for each button, KeyButton2, 3,4 etc)
-Press OK button

This way, you will have the same Base Class for all buttons, and flash will create the other classes for you.
0
 
wildbrookmediaCommented:
I think you need to right click on the movie clip symbol in the library and select linkage, then give the mc symbol that is your button the id of KeyButton.
0
 
keybordAuthor Commented:
Thanks wildbrook.  Though, unless I misunderstand you, Im afraid thats not quite advanced enough of a solution...I have already tried linking the script like you suggest but as I say I have six buttons and when I try to link the script to anything other than the first one I get the warning that I have to enter " ...a unique class name not associated with any other symbols"

i.e. I have one 'KeyButton.as' file that I am referencing and I am trying to link that single file (and therefore the functions within it) to six different movie clip buttons in the .fla.  It doesnt matter which button I link to first but after that I get this warning and am unable to link the rest.

thanks for your thoughts
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
wildbrookmediaConnect With a Mentor Commented:
Does each of your buttons on the stage have a different unique instance name associated with it?
0
 
keybordAuthor Commented:
Yes.  I have attached a couple of screenshots in the off chance the help explain anything better. - Im on a Mac - hope you wont hold it against me...;o)

The code from the KeyBord.as file (which you can see tabbed in the scr shot) is as above

sc1: You can see the linkage I am talking about and the timeline of one of the buttons that the script is meant to control.

sc2:  You can see the layout of the main timeline and that each button has a unique instance name.



sc1.jpg
sc2.jpg
0
 
julianopolitoCommented:
01.28.2008 at 04:03PM BRT, ID: 20761848
Here is the answer.

-Put the .as file in the same folder as the .fla.
-Open the fla, open the library .
-Right Click the KeyButton movieclip in the library.
-Select Linkage...
-Check Export for Actionscript
-In the Class field, write KeyButton (it is case sensitive)
-Press OK button

Now you have associated the class file with the movieclip symbol. That means your movieclip is now registered as a KeyButton.

Now you do not have to import anything. Just use your movieclip as usual. If you want to create a new KeyButton from actionscript, there is no need to import anything, since its package is empty, it is in the same folder as the fla.

Do the following:

var newKey:KeyButton = new KeyButton();//Create the key button instance
addChild(newKey);//Add the keybutton to the display list

Remeber that KeyButton is also a movieclip, cause it extends MovieClip. So you have all the same functionality here.
0
 
wildbrookmediaCommented:
The .as file is not in the same directory as the .fla?
0
 
keybordAuthor Commented:
Thanks for your time here guys.  

Your last comment sealed this query for me I think Julian and is a nice way of using just the one class.  (Though I confess I dont quite understand how I can get away with subsituting the Base class 'flash.display.MovieClip' for my own class but I'm assuming thats cause my own class is an 'extension' of the Movieclip class?)

Anyway this has turned into a very nice primer on AS3 classes and I will keep on with the homework!

Dont be surprised to hear from me again in a few days. = )
0
 
julianopolitoCommented:
You can change the base class because your class EXTENDS flash.display.MovieClip, so it means your class is a specialized version of MovieClip. That's why you can do that. Then when you specify an non existent class to be the symbol class, flash creates an empty class for automatically. This class extends the base class KeyButton
0
All Courses

From novice to tech pro — start learning today.