Link to home
Start Free TrialLog in
Avatar of keybord
keybord

asked on

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

Avatar of wildbrookmedia
wildbrookmedia
Flag of United States of America image

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.
Avatar of keybord
keybord

ASKER

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
SOLUTION
Avatar of wildbrookmedia
wildbrookmedia
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of keybord

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The .as file is not in the same directory as the .fla?
Avatar of keybord

ASKER

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. = )
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