初识AS3(十五)——导航栏

知识点]
1、AS创建按钮 upState、overState、 downState、 hitTestState
2、打开网页 navigateToURL()
3、用for创建的若干个按钮,得到对应的数,便于取得数组中对应的某元素 var b:SimpleButton=event.target as SimpleButton;
4、运用Timer类创建按钮指示移动
[效果]


初识AS3(十五)——导航栏导航栏.swf

CODE:


//设置帧速
stage.frameRate=30
//申明一些变量
var namearray:Array=new Array("闪吧","动漫","教材","素材","论坛");
var urlarray:Array=new Array("http://www.flash8.net","http://www.flash8.net/flash",
"http://www2.flash8.net/teach","http://www.flash8.net/download.shtml",
"http://space.flash8.net/bbs");
var btn:SimpleButton;
var k:uint;
var url:URLRequest;
var t:Timer=new Timer(30);
var X0:Number;
var Y0:Number;
//创建按钮
for (k=0; k<namearray.length; k++) {
btn=new SimpleButton();
btn.x=50*k+100;
btn.y=50;
btn.upState=hua(0x006600);
btn.overState=hua(0x009900);
btn.downState=hua(0x006600);
btn.hitTestState=btn.upState;
btn.addEventListener(MouseEvent.MOUSE_MOVE,jg);
btn.addEventListener(MouseEvent.CLICK,dj);
var txt:TextField=new TextField();
txt.autoSize=TextFieldAutoSize.CENTER;
txt.textColor=0xffffff;
txt.text=namearray[k];
txt.x=(btn.width-txt.width)/2+btn.x;
txt.y=(btn.height-txt.height)/2+btn.y;
addChild(btn);
addChild(txt);
txt.mouseEnabled=false;
}
function hua(color:uint):Shape {
var circle:Shape=new Shape();
circle.graphics.beginFill(color);
circle.graphics.drawRect(0,0,50,20);
circle.graphics.endFill();
return circle;
}
//按钮指示
var zs:Sprite=new Sprite();
zs.graphics.beginFill(0x009900);
zs.graphics.moveTo(-5,15);
zs.graphics.lineTo(5,15);
zs.graphics.lineTo(0,10);
zs.graphics.lineTo(-5,15);
zs.graphics.endFill();
zs.x=125;
zs.y=35;
addChild(zs);
//按钮指示的坐标设置
t.addEventListener("timer",js);
function js(e:TimerEvent):void {
var dx:Number=X0-zs.x;
var dy:Number=Y0-zs.y;
var jl:Number=Math.sqrt(dx*dx+dy*dy);
if (jl<1) {
  zs.x=X0;
  zs.y=Y0;
  t.stop();
} else {
  var vx:Number=dx*.2;
  var vy:Number=dy*.2;
  zs.x+=vx;
  zs.y+=vy;
}
}
//鼠标经过按钮
function jg(e:MouseEvent):void {
X0=e.target.x+25;
Y0=e.target.y-15;
t.start();
}
//鼠标按下按钮
function dj(e:MouseEvent):void {
var b:SimpleButton=e.target as SimpleButton;
url=new URLRequest(urlarray[getChildIndex(b)/2]);
navigateToURL(url);
}

你可能感兴趣的:(初识AS3(十五)——导航栏)