Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Adding the ButtonPressedEvent Class to the DTK_Button Class

December 7th, 2009 · No Comments · AS3 Custom Classes

In the last post, I introduced the DTK_Button class but left out a means for programmatically determining when the button is pressed. In this post, the ButtonPressedEvent class is used to determine when a button is clicked.

Examining the code of the modified button class (included in the download), you will notice two new modifications to the class.  Firstly, a new parameter, btID which is used to identify one button from another, has been added to the constructor.  Secondly, a new line of code has been added to the onClick function which sends out the id of the button pressed to available event listeners.

The code below implements 3 instances of the DTK_Button class, each with its own event listener calling the same function.

import com.dtk.DTK_Button;
import com.dtk.ButtonPressedEvent;  // new import statement
 
// array: 'textFont', textSize, textBold, textXOffset, textYOffset
var textArray:Array  = [ 'Verdana', 14, true, 7, 4 ];
 
// array:  baseLayerColor, rolloverLayerColor, clickedLayerColor, textBaseColor, textRolloverColor
var colorArray:Array = [ 0x009966, 0x006699, 0xffcc00, 0xffffff, 0xffffff ];
 
 
/**
 * create 3 instances of DTK_Button class - each with its own event listener calling same function
 *
 */
var tb1:DTK_Button = new DTK_Button ( 1, 'Btn 1', 60, 32, 32, textArray, colorArray );
tb1.x = 10;
tb1.y = 10;
tb1.addEventListener ( ButtonPressedEvent.BUTTON_PRESSED, onButtonPressed );
addChild( tb1 );
 
var tb2:DTK_Button = new DTK_Button ( 2, 'Btn 2', 60, 32, 32, textArray, colorArray );
tb2.x = 10;
tb2.y = 50;
tb2.addEventListener ( ButtonPressedEvent.BUTTON_PRESSED, onButtonPressed );
addChild( tb2 );
 
var tb3:DTK_Button = new DTK_Button ( 3, 'Btn 3', 60, 32, 32, textArray, colorArray );
tb3.x = 10;
tb3.y = 90;
tb3.addEventListener ( ButtonPressedEvent.BUTTON_PRESSED, onButtonPressed );
addChild( tb3 );
 
// function to respond to button clicked
function onButtonPressed ( e:ButtonPressedEvent ):void
{
	message.text = 'Button No. ' + e.buttonEvent + ' pressed';
}

Download files: Buttons.zip (83)

If you found this post helpful please consider a Retweet.  Thanks…

Post to Twitter

Tags: ···

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

Spam Protection by WP-SpamFree