打开组件面板的快捷键是Ctrl+F7,要使用组件要做的第一件事就是把组件放到库中,最简单的办法就是,从库中把组件拖到舞台上,然后再把它删掉。然后打开库面板,查看是否放入库中,然后我们就可以在as代码里面调用Button组件了。
import fl.controls.Button;
var bt:Button = new Button(); //实例化一个Button按钮组件实例
stage.addChild(bt); //把Button组件实例放到舞台
要更改bt按钮上面显示的文本,需要修改bt的label属性,如果觉得按钮上面的字体太普通,我们可以通过TextFormat对象修改bt实例的样式:
import fl.controls.Button;
import flash.text.TextFormat;
var tf:TextFormat = new TextFormat();
tf.size = 12;//设置字体大小
tf.color = 0xFF00FF;//设置字体颜色
tf.bold = true;//设置字体粗细
var bt:Button = new Button();
bt.x = 100;
bt.y = 100;
stage.addChild(bt);
bt.label = "确定";
bt.setStyle("textFormat",tf);//对tf设置样式
利用按钮组件可以进行互动操作,比如按下时画圆:
import fl.controls.Button; import flash.text.TextFormat; import flash.events.MouseEvent; import flash.display.Bitmap; import flash.display.Sprite; var tf:TextFormat = new TextFormat(); tf.size = 12; tf.color = 0xFF00FF; tf.bold = true; var bt:Button = new Button(); bt.x = 100; bt.y = 100; stage.addChild(bt); bt.label = "确定"; bt.setStyle("textFormat",tf); bt.addEventListener(MouseEvent.CLICK,changeStageColor); function changeStageColor(e:MouseEvent):void{ var s:Sprite = new Sprite(); s.graphics.beginFill(0xFF0000,1); s.graphics.drawCircle(Math.random()*500,Math.random()*450,Math.random()*40); s.graphics.endFill(); stage.addChildAt(s,0); }
点击按钮,就会画出随机大小和坐标的圆……