Game Development Reference
In-Depth Information
follows; note that we will pass in the instance of the
buttonTextBitmapData
. This becomes the
bitmapData
property of the
buttonTextBitmap
:
buttonTextBitmap = new Bitmap(buttonTextBD);
When centering the text on the button we have to compensate for an unchangeable, invisible 2-
pixel buffer around at the top and left of the
tempText (TextField object)
when it is drawn into
buttonTextBitmapData
. We could use a matrix operation to translate the position of the text when
it is drawn, but for simplicity, we will simply subtract two pixels from the
x
and
y
position to
compensate for this buffer. We calculate the center
x
and
y
positions like this:
//X: (the background width - the text width)/2 minus the 2pixel buffer
//Y: (the background height - the text height)/2 minus
//the 2pixel buffer
buttonTextBitmap.x = ((buttonBackGroundBitmap.width - int(tempText.textWidth))/2)-2;
buttonTextBitmap.y = ((buttonBackGroundBitmap.height -
int(tempText.textHeight))/2)-2;
The second-to-last thing we do to the button is to add both the
buttonBackGroundBitmap
and then
the
buttonTextBitmap
to the
Sprite
's display list in that order. The last thing we do is to set the
handCursor
and
buttonMode
for this
Sprite
to
true
. This will allow the hand cursor to show up
rather than the arrow when the mouse is over our buttons.
The changeBackGroundColor function definition
The
changeBackGroundColor
function is used to swap out the color under the text of the button
when moving between the
OFF
and
OVER
states and to swap out the background on rollover. A
constant is passed in; either
OFF
or
OVER
is used in the
if
clause to set the
bitmapData
property of
the
buttonBackGroundBitmap
to the corresponding
BitmpData
instance for the background that we
created in the constructor.
The CustomEventButtonId class
The
CustomEventButtonId
class is used to pass data to an event listener when an event occurs. It
is a relatively simple class that extends the built-in
Event
class. Its primary function is to allow the
passing of a simple integer value along with the dispatched
Event
instance.
Create the
CustomEventButtonId.as
file in the framework package structure we created earlier:
/source/classes/com/efg/framework/CustoEventButtonId.as
Here is entire code listing for this class:
package com.efg.framework
{
import flash.events.*;
/**
* ...
* @author Jeff Fulton
*/
public class CustomEventButtonId extends Event {
public static const BUTTON_ID:String = "button id";
public var id:int;