Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Create a button with Actionscript 3.0

This is a discussion on Create a button with Actionscript 3.0 within the Tutorials forums, part of the Flash English category; Another new feature of Actionscript 3.0 is the SimpleButton Class. It is basically the old Actionscript 2.0 Button ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 29-09-07, 10:01
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Create a button with Actionscript 3.0

Another new feature of Actionscript 3.0 is the SimpleButton Class. It is basically the old Actionscript 2.0 Button Class but with a few new Methods.
With Flash CS3, it is possible to create buttons in runtime using Actionscript and act on the 4 states of the button which as been introduced as methods:
- upState
- overState
- downState
- hitTestState
In fact, if we think about it, the classic Flash button symbol always has had 4 keyframes in which we could control its different states.

Let's see how to handle them with Actionscript'

I create a FLA and save it as 'bottone_fla' (button_fla)
I create a Document Class, an AS file saved as 'Bottone.as' (button.as), implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Bottone extends MovieClip
	{
		private var my_button:SimpleButton;
		
		public function Bottone()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			creaBottone();
			addChild(my_button);
			aggiungiListener();
		}
		
		private function creaBottone():void
		{
			my_button=new SimpleButton();
			my_button.x=50;
			my_button.y=50;
			
			my_button.upState=disegnaCerchio(25);
			my_button.overState=disegnaCerchio(35);
			my_button.downState=disegnaCerchio(50);
			my_button.hitTestState=my_button.upState;
		}
		
		private function disegnaCerchio(raggio:Number):Shape
		{
			var cerchio:Shape=new Shape();
			cerchio.graphics.beginFill(0x000000,1);
			cerchio.graphics.drawCircle(my_button.x,my_button.y,raggio);
			cerchio.graphics.endFill();
			return(cerchio);
		}
		
		private function aggiungiListener():void
		{
			my_button.addEventListener(MouseEvent.CLICK,cliccato);
			function cliccato(m:MouseEvent):void
			{
				trace('ok');
			}
		}
	}
}
The result:







Let's take a look at the code:
We shall concentrate on the init method of the Class Bottone :
- first of all, we call the creaBottone method
- then, we add the button to the Display Object (otherwise it would be invisible)
- I add a listener awaiting the click of the mouse on the button.
What's the use of the creaBottone method:
I create a button
my_button=new SimpleButton();
I position it
my_button.x=50;
my_button.y=50;
I assign to the upState (neutral state), which is nothing else that the upState method of the button just created, the function disegnaCerchio to which I pass the radius of the circle as a numerical value. The function will draw the circle and return an instance of the Shape Class.
my_button.upState=disegnaCerchio(25);
I assign to the overState (the state when the mouse touch the button), which is nothing else that the overState method of the button just created, the function disegnaCerchio to which I pass the radius of the circle as a numerical value. The function will draw the circle and return an instance of the Shape Class.
my_button.overState=disegnaCerchio(35);
I assign to the downState (the state when the mouse is clicked on the button), which is nothing else that the downState method of the button just created, the function disegnaCerchio to which I pass the radius of the circle as a numerical value. The function will draw the circle and return an instance of the Shape Class.
my_button.downState=disegnaCerchio(50);
To the method hitTestState (the hit zone into which will be recognised the click of the mouse), I assign the upState method (neutral) of the same button
my_button.hitTestState=my_button.upState;

Let's see what the function disegnaCerchio does:
I create an instance of the Shape Class
var cerchio:Shape=new Shape();
I start the drawing of the graphics
cerchio.graphics.beginFill(0x000000,1);
I draw a circle with a radius of the value passed directly to the function
cerchio.graphics.drawCircle(my_button.x,my_button. y,raggio);
I close the drawing parts of the graphics
cerchio.graphics.endFill();
I return the shape
return(cerchio);

So, for each state of the button, I use an unique function to which I pass each time (depending on the state) a different value of the radius of the circle which will be designed by the function.

What the use of the function aggiungiListener:
I add a listener to the click of the mouse on the button and perform an action (in this case the trace)
my_button.addEventListener(MouseEvent.CLICK,clicca to);
function cliccato(m:MouseEvent):void
{
trace('ok');
}

See you soon!
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 07:00..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
How to create drop down menu IKONG advanced Actionscript 3.0 2 06-08-08 03:30
Actionscript 3 Create a loader with a clip manheman advanced Actionscript 3.0 1 20-06-08 10:01
Actionscript 3 How to create separate motions? Bahman Actionscript 3.0 newbies 7 15-06-08 08:05
Create special space-effects Anxiro2 Flash English 0 15-04-08 08:37
How to create a clock using Flash CS3 and PHP - second part Flep Tutorials 0 25-09-07 17:26


All times are GMT. The time now is 12:32.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap