Graphics Reference
In-Depth Information
property of a Sprite to create an invisible button that covers the stage
as in Example 3.3. Note that I also included the ClickTagger class we
discussed earlier and that this is an example of timeline code.
EXAMPLE 3.3
Creating an invisible button
import flash.display.Sprite;
import com.flashadbook.utils.ClickTagger;
var fallBackUrl:String =
“
http://www.flashadbook.com
”
;
var clickTagger:ClickTagger = new
ClickTagger(stage.loaderInfo);
var myBtn:Sprite = new Sprite();
var lineThickness:int = 1;
myBtn.graphics.lineStyle(lineThickness, 0x000000);
myBtn.graphics.beginFill(0x000000, 0);
myBtn.graphics.drawRect(0, 0, stage.stageWidth-
lineThickness, stage.stageHeight-lineThickness);
clickTagger.assignClickTag(myBtn,fallBackUrl,1);
addChild(myBtn);
InExample3.3,thefirstfewthingswedoaretocreateanew
instance of ClickTagger, a Sprite named
myBtn
, and an int called
lineThickness
. Next, we use the graphics property of the Sprite to
draw a rectangle that has a black outline and a transparent fill.
This is done with
lineStyle
,
beginFill
,and
drawRect
.Whenwe
use the
drawRect
method here, the first two parameters represent
the x and y coordinates of the upper-left corner of our button,
whereas the second two parameters represent their width
and height. The reason for subtracting
lineThickness
from the
stageWidth
and
stageHeight
is because the button will actually be
drawn exactly to the edge of the stage and that ends up putting the
right and bottom of the border outside of the visible area. So that
'
s
it. There
s not a lot to it, and you can easily move this chunk of
code from banner to banner. You could carry this a little further
and create a BorderButton class as in Example 3.4.
'
EXAMPLE 3.4
The BorderButton class
package com.flashadbook.display {
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.external.ExternalInterface;
import flash.system.Capabilities;