Game Development Reference
In-Depth Information
height
of the button
text
String
for the button's label
Color for the
OFF
background (as a
uint
)
Color for the
OVER
background (as a
uint
)
TextFormat
object we pass into the button for styling the text.
positionOffset
value representing a value that will be used to bump the button text
back toward the upper-left if needed
After setting the
x
and
y
values for the button, we move on to creating backgrounds for the
OFF
and
OVER
states with simple
BitmapData
objects. The
width
,
height
, and colors (
offColor
and
overColor
) passed in are used to create simple opaque
BitmapData
rectangles in the appropriate
size. The
buttonBackGroundBitmap
, which is the on screen display holder for the background, is
set to use the
OFF
bitmapData
by default.
buttonBackGroundBitmap = new Bitmap(offBackGroundBD);
After the background, we move on to the text. The goal is to place the text in its own
BitmapData
instance and center it (if possible) on the button. We create the
tempText TextField
that will be
used to set up and format the
BitmapData
text.
In this simple button, we don't check to see if the text will fit on the button, so if the
width
and
height
are not sufficient to house the width and height of the text, it will simply be truncated. The
BitmapData
instance that holds the text will not let the text run off the sides, as
BitmapData
does
not have any space outside of its viewable area (unlike the
Stage
or
Sprites
and
MoveClips
). The
final thing we do to the
tempText
field is to set its
textFormat
property to the passed in
textformat
.
Now, we move on to placing the text on the button and centering it. This takes a little extra
measuring, as we will find that in some cases there is a little 2-pixel margin in
TextField
objects
that we have to compensate for when we create the
BitmapData
holder for our
tempText
field. We
used the passed in
positionOffset
for this. We will use the passed in actual width and height of
the
tempText
field (the
textWidth
and
textHeight
values), a transparent background (
true
) and
no background color (
0x00000000
):
buttonTextBD = new BitmapData(tempText.textWidth,
tempText.textHeight, true, 0x00000000);
The
0x00000000
is a 32-bit integer that represents the
AARRBBGG
color values. The
AA
is the
transparency value. By setting it to
00
, we effectively create an invisible background, no matter
the color (black in this instance).
To get the
tempText.text
value into the
buttonTextBitmapData
BitmapData
instance, we simply
call the
draw
method of the
buttonTextBitmapData
and pass in the
tempText
variable:
buttonTextBitmapData.draw(tempText);
The
draw
method takes the contents of any display object and paints the current set of pixels into
the
BitmapData
object. The
draw
method can be used to take pretty much any vector (or bitmap
contents) of a display object and effectively cache them for use later. This method is notoriously
slow, but we won't use it often in actual game play. We use it to set up our objects for later use.
The final thing we must do with our text for the button is to add it to an actual display object. We
have created a
Bitmap
instance holder for the text and have called it
buttonTextBitmap
. This is
the display object that will be layered on top of the
buttonBackGroundBitmap
to create the illusion
of a clickable button with
OFF
and
OVER
states. The line of code that creates the
buttonTextBitmap