var mytxt:TextField = new TextField(); //建立文字框
mytxt.border = true;
mytxt.text = "HelloWorld";
mytxt.x = 150;
mytxt.y = 20;
mytxt.visible = false;
addChild(mytxt);
function createRect(x1,y1,w1,h1,borderSize,borderColor,bgColor){
var myButton:Sprite = new Sprite(); //建立按鈕
myButton.buttonMode = true;
addChild(myButton);
var mc:Shape = new Shape(); //建立形狀
mc.graphics.lineStyle(borderSize, borderColor);
mc.graphics.beginFill(bgColor);
mc.graphics.drawRect(x1, y1, w1, h1);
mc.graphics.endFill();
myButton.addChild(mc); //指定為按鈕
myButton.addEventListener(MouseEvent.CLICK, clickBtn); //建立監聽器
}
createRect(150,150,100,40,2,0x000000,0xFFCC00);
function clickBtn(myMouseEvent:Event):void{
if (mytxt.visible){
mytxt.visible = false;
}else{
mytxt.visible = true;
}
}
本範例把矩形指定為按鈕,(變數:資料類型) 宣告變數時同時指定資料類型也是寫 ActionScript 的好習慣,以後檢查只要一看就清楚明瞭。 mytxt.border = true;
mytxt.text = "HelloWorld";
mytxt.x = 150;
mytxt.y = 20;
mytxt.visible = false;
addChild(mytxt);
function createRect(x1,y1,w1,h1,borderSize,borderColor,bgColor){
var myButton:Sprite = new Sprite(); //建立按鈕
myButton.buttonMode = true;
addChild(myButton);
var mc:Shape = new Shape(); //建立形狀
mc.graphics.lineStyle(borderSize, borderColor);
mc.graphics.beginFill(bgColor);
mc.graphics.drawRect(x1, y1, w1, h1);
mc.graphics.endFill();
myButton.addChild(mc); //指定為按鈕
myButton.addEventListener(MouseEvent.CLICK, clickBtn); //建立監聽器
}
createRect(150,150,100,40,2,0x000000,0xFFCC00);
function clickBtn(myMouseEvent:Event):void{
if (mytxt.visible){
mytxt.visible = false;
}else{
mytxt.visible = true;
}
}
| 2007/08/09 18:43 |
Trackback Address :: http://blog.raienet.com/trackback/354
