Tutorial Create a reusable button class in Actionscript 3 using Flash CS3

Wednesday, August 29, 2007 2:19

This tutorial will teach you how to create a reusable button class to use in all your futureactionscript projects. Creating custom classes and writing reusablecode are the key factors that elevate AS3 from AS2 aside from speed and performance that is. classes when written are saved as .as files andcan be called into action with a simple copy paste and inner class call. This Tutorial requires you have a knowledge of programming and prior knowledge of how to use flash.

 
 


Your Ad Here

Firstly start off by opening 2 files one .fla file and the other a .as file and name them as button.fla and buttonTute.as make sure they are in the same folder. Set the stage colour to a darkish grey for added visibility.

 

 

Then make sure that the name of your .as file is set up as your document class this is so the code is runs at runtime with flash player. All you do is type the name of your main .as file into the illustrated text field within the properties pannel.

 Thats all the prep work done now we get into the coding side of things. Not to worry all shall be explained

 

first off set up the package that all the codes going to be inside

 package{

Then i imported all the classes i was going to be using within this custom button class

it is important that you use the import to basically have all the ones you need listed and ready for runtime



    import flash.display.MovieClip

    import flash.display.Shape

    import flash.display.SimpleButton

    import flash.events.MouseEvent

Then name the class the same as the file name remember this is important so that everything works correctly. class names should always be the same as file names



    

    public class ButtonTute extends MovieClip{

And the on with the constructor function named the same



        

        public function ButtonTute() {

Here i set a variable button with functionality of SimpleButton this carries the states of a button like the upState and downState as im sure you have come across when using buttons in photoshop, flash, fireworks.



        

        var    button:SimpleButton = new SimpleButton();

        

Here i have set the variable up as Shape that means its more like a movie clip and a Sprite

then drawn out a simple black square with a white border along with adding it to the stage for runtime using addChild()

this code has set the state of the button in its upState



        

        var up:Shape = new Shape()

        up.graphics.lineStyle(0, 0xFFFFFF, 1);

        up.graphics.beginFill (0×000000);

        up.graphics.drawRect (0, 0, 100, 50);

        up.graphics.endFill();

        button.upState = up;

        button.x = 200

        button.y = 200

        addChild(button);

The same process was repeated for the over state note that all the settings were changed over to the new var for the overState cleverly named over.



        

        var over:Shape = new Shape()

        over.graphics.lineStyle(0, 0xCCCCCC, 1);

        over.graphics.beginFill (0×000000);

        over.graphics.drawRect (0, 0, 120, 60);

        over.graphics.endFill();

        button.overState = over;

        

and the same again for the down state



        var down:Shape = new Shape()

        down.graphics.lineStyle(0, 0xCCCCCC, 1);

        down.graphics.beginFill (0×000000);

        down.graphics.drawRect (0, 0, 80, 40);

        down.graphics.endFill();

        button.downState = down;

A hitTestState is not visible on the stage at any point its a sort of container if you like that sets the hitTestState



        

        button.hitTestState = up;

Now we do have to set this setting to true as the default is set to false so that we can also trace a double click as well as any others



        

        button.doubleClickEnabled = true;

        

Here i have added event listeners for each of the types of mouse events we want to check the button. The trace messeges we will set for each of these will only be triggered when the button is click.



        button.addEventListener(MouseEvent.CLICK, onClick)

        button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);

        button.addEventListener(MouseEvent.MOUSE_UP, mouseUp);

        button.addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);

        }

        

I set each of the functions along with relevant trace messages


    private function onClick (event:MouseEvent):void{

        trace ("Click");

    }

    private function mouseDown(event:MouseEvent):void{

        trace ("mouse down");

    }

    private function mouseUp (event:MouseEvent):void{

        trace ("Mouse up");

    }

    private function onDoubleClick (event:MouseEvent):void{

        trace ("Double clicked");

    }

    

    }

}

 

And there we have a very simple yet Reusable button class

 

please comment and request a tutorial

Compare IVA Plans

Compare your options using our free
calculator - Search online now!

www.trapped.co.uk

Matched.co.uk

Similar Posts:

Share/Save/Bookmark

You can leave a response, or trackback from your own site.

8 Responses to “Tutorial Create a reusable button class in Actionscript 3 using Flash CS3”

  1. Fred Griffith says:

    September 29th, 2007 at 1:24 am

    I am learning Flash while creating an animated Website for a client. I just upgraded to CS3 and now none of my buttons work. I tried saving as Flash 8, but they still will not work using ActionScript 2 either…

    Please show us how (from the beginning) to create navigation buttons (play, pause, next, gotoandplay, etc.) that will work in ActionScript 3.0. I can learn the principles later. Right now, I have got to get my client’s buttons working again!

    If you would, please email your response to me as well as having it on your Website.

    Thank you.

  2. Travelogue » miraz tutorials says:

    December 9th, 2007 at 3:51 pm

    [...] http://www.miraztutorials.com/tutorial-create-a-reusable-button-class-in-actionscript-3-using-flash-... is a blog written and maintained by a writer, reader and thinker. also an expert on computer design technology. [...]

  3. rakesh says:

    December 18th, 2007 at 4:21 pm

    hi..i want to link a .msi file in flash ..how can i do it .plz tell me ..i know the fundamentals of flash,ie how to use a tool etc..so plz send the detailed tutorial ..

  4. poonam says:

    July 26th, 2008 at 7:00 pm

    pls tell me how to create media player in flash

  5. giechika says:

    September 10th, 2008 at 9:02 pm

    In this code, there are 3 changes that need to be made. All of them are the same.

    Inside the color line with the beginFill, the x is not an x but the multiplier symbol. It needs to be changed to the x above or the compiler will throw errors.

    George

    over.graphics.lineStyle(0, 0xCCCCCC, 1);

    over.graphics.beginFill (0×000000);

  6. Mats says:

    September 25th, 2008 at 1:17 pm

    Hi there,

    thx for the tutorial.

    I finally made it work but it took a few more changes to the code than those described by gieschika.

    1) Some semicolons are missing - if it really affected the functionality I’m not too sure.

    2) As described by the author, the filename should be equal to the class name - in this case “ButtonTute”. Copy-pasting the code will result in an error since the user is told to call his file “buttonTute.as” and not “ButtonTute.as”

  7. oldenbka says:

    October 16th, 2008 at 9:32 pm

    Hi,

    I am new to flash and am creating a flash image with clickable buttons for a survey. The image has 10 areas which can be set up as buttons. Is there a way that I can assign a value for each “button” and send it to communicate with an html document? (when I click on a button, a value for that button, say “1″ is then sent to my html and generated in a text field.)

  8. shaz says:

    October 22nd, 2008 at 8:31 pm

    Hi Oldenbka the way to do that would be using php and get flash to send variables to be output as text via xml

Leave a Reply