Bitmap 类表示用于表示位图图像的显示对象。这些图像可以是使用 flash.display.Loader 类加载的图像,也可以是使用 Bitmap() 构造函数创建的图像。本篇博文中,选择库中图片,设置为Actionscript3.0导出新类,这样就创建了以 BitmapData为基类的自定义类。 Bitmap() 构造函数包含对这个自定义类的实例引用。创建了 Bitmap 对象后,可以使用 addChild() 或 addChildAt() 方法将位图放在显示列表中。
首先,导入了图片后,右击图片,选择属性,勾选“为ActionScript导出”,在标识符类框里,写上你想要的类的名字。本实例中,自定义了两个类,分别是Big类和Small类。在动作面板中我们就可以像处理内建类那样,使用构造函数类创建实例,并将实例名传递为Bitmap类的实例就可以加入显示列表中了。
但是,Bitmap 类不是 InteractiveObject 类的子类,因此它无法调度鼠标事件。 本实例中我们创建了Sprite实例,将 Bitmap 对象包含在Sprite类的显示对象容器中,这样我们就可以自由地使用 addEventListener() 方法了。
在这个实例中的,我给图片加了三个鼠标事件,一个鼠标移入事件(MOUSE_MOVE),一个鼠标移出事件(MOUSE_OUT),还有一个鼠标单击事件(CLICK)。
Code:
var big:Big=new Big(100,75);
var small:Small=new Small(50,35);
var bigpic:Bitmap=new Bitmap(big);
var smallpic:Bitmap=new Bitmap(small);
var bigspr:Sprite=new Sprite();
var smallspr:Sprite=new Sprite();
bigspr.addChild(bigpic);
smallspr.addChild(smallpic);
bigspr.scaleX=.6;
bigspr.scaleY=.6;
var i:int=1;
chuli(smallspr,110,150);
smallspr.addEventListener(<a href=http://www.flashabc.net/catalog.asp?tags=MouseEvent>MouseEvent</a>.MOUSE_MOVE,mousein);
smallspr.addEventListener(MouseEvent.MOUSE_OUT,mouseout);
smallspr.addEventListener(MouseEvent.CLICK,mousedown);
function mousein(e:MouseEvent):void {
chuli(bigspr,90,0);
}
function mouseout(e:MouseEvent):void {
removeChild(bigspr);
}
function mousedown(e:MouseEvent):void {
smallspr.removeEventListener(MouseEvent.MOUSE_MOVE,mousein);
smallspr.removeEventListener(MouseEvent.MOUSE_OUT,mouseout);
smallspr.removeEventListener(MouseEvent.CLICK,mousedown);
removeChild(bigspr);
smallspr.scaleX=1.5;
smallspr.scaleY=1.5;
smallspr.x=75;
smallspr.y=70;
}
function chuli(mypic:Sprite ,ax:<a href=http://www.flashabc.net/catalog.asp?tags=Number>Number</a>=0,ay:Number=0) {
addChild(mypic);
mypic.x=ax;
mypic.y=ay;
}