网站制作学习网Flash→正文:as3.0 flash动态添加按钮
字体:

as3.0 flash动态添加按钮

Flash 2011/8/9 19:02:49  点击:不统计


as中没有动态生成自定义按钮,如果通过已经有的类来生成按钮,不太适合使用。我通过变换方法实现了actionScript动态添加按钮,并自定义按钮样式。
思路是这样的: 关键词:flash动态添加按钮  自定义flash按钮文字,as添加flash按钮
通过动态创建影片剪辑,然后给影片剪辑添加text文本框,然后在进行 鼠标,事件的添加。代码如下
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.sensors.Accelerometer;
//前面是引入了需要的类
var color =0xFF0000;//临时定义了按钮颜色
function makeShapes(i):void {//创建函数(Ellipse类下面已经给出,是画圆的一个类)
var ellipse:Ellipse = new Ellipse(50,50,color);//创建圆高和宽都是50,可以不同,则为椭圆影片剪辑
 var textForaspcn:TextField = new TextField();//创建文本框
 textForaspcn.htmlText = "点击"+i+"按钮<br>";//显示内容
 textForaspcn.x =2;//定义文本框在创建的影片剪辑位置
 textForaspcn.y =10;
 textForaspcn.height=20;//定义文本框宽和搞
 textForaspcn.width=50;
    textForaspcn.addEventListener(MouseEvent.MOUSE_OVER,FORASPCN_IN);//当鼠标在文本框上面时则显示小手
 textForaspcn.addEventListener(MouseEvent.MOUSE_OUT,FORASPCN_OUT);//当鼠标在文本框外面时则显示正常的
 ellipse.addChild(textForaspcn);//文本框添加到影片剪辑
 ellipse.buttonMode = true;     //新影片剪辑设置按钮模式
 addChild(ellipse);//添加到场景
 ellipse.addEventListener(MouseEvent.CLICK,aaa);//定义点击事件
 ellipse.x = stage.width * Math.random();//随机一个位置
 ellipse.y = stage.height * Math.random();
}
function aaa(me:MouseEvent):void{
   this.setChildIndex(msg,this.numChildren-1);
   msg.text = "你点击了按钮";
 }
function FORASPCN_IN(me:MouseEvent):void{
Mouse.cursor = MouseCursor.BUTTON;
}
function FORASPCN_OUT(me:MouseEvent):void{
Mouse.cursor = MouseCursor.AUTO;
}
for(var i:Number=0;i<5;i++)makeShapes(i);

下面是Ellipse类
package {
 import flash.display.MovieClip;
 public class Ellipse extends MovieClip {
  public function Ellipse(w:Number=40,h:Number=40, color:Number=0xff0000) {
  graphics.beginFill(color);
  graphics.drawEllipse(0, 0, w, h);
  graphics.endFill();
 }
}

}
下载全部 as3.0 flash动态添加按钮


·上一篇:动态添加影片剪辑实例 >>    ·下一篇:flash actionScript3.0 贪吃蛇程序 >>
推荐文章
最新文章