,右键菜单中选"Component Definition"(组件定义)选项,会进入一窗口,把类名填上"MyButton",再把"diaplay in Components panel"选项打上钩,"Tool tiptext"中填入组件名称,我这里填"MyButton".这里还有设置组件的preview(预览)效果还有图标等,其他内容大家可根据需要自行设置。
7、到这步,我们会发现还缺少一个外部的自定义组件的代码,也就是上面"MyButton"元件连接的"MyButton"类.由于类中导入了组件类,所以我们要设置FLASH CS3的classPath(类路径),选编辑-->参数设置-->ActionScript-->ActionScript3.0,添加一新的类搜索路径,我这里为"D:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\Component Source\ActionScript 3.0\User Interface"
大家根据各自情况设置好路径,新建ActionScript文档,填入如下代码:
/************MyButton.as**************/
package{ import flash.display.*; import flash.events.*; import flash.system.ApplicationDomain; import fl.core.UIComponent; public class MyButton extends UIComponent { private var nowSkin:MovieClip; private var thisDomain:ApplicationDomain; public function MyButton() { super(); trace("MyButton"); } override protected function draw():void { if (nowSkin==null) { thisDomain=loaderInfo.applicationDomain; var classDef try{ classDef=thisDomain.getDefinition(getNormalSkinName());} catch(e:ReferenceError){ trace("没有在库中找到相关的类!") return; } nowSkin=new classDef as MovieClip; nowSkin.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler); addChild(nowSkin); } nowSkin.width=width; nowSkin.height=height; } protected function mouseOverHandler(e:MouseEvent):void{ trace("over"); removeChild(nowSkin); var classDef=thisDomain.getDefinition(getOverSkinName()); nowSkin=new classDef as MovieClip; addChild(nowSkin); nowSkin.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler); draw(); } protected function mouseOutHandler(e:MouseEvent):void{ trace("out"); removeChild(nowSkin); var classDef=thisDomain.getDefinition(getNormalSkinName()); nowSkin=new classDef as MovieClip; addChild(nowSkin); nowSkin.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler); draw(); } protected function getNormalSkinName():String { return "NormalSkin"; } protected function getOverSkinName():String { return "OverSkin"; }
} } |
|