通过实例学习Flash AS3.0——案例五

2019-10-08 19:02:37刘景俊

代码稍微有点长,且听我细细道来
package todd.interactive{
import flash.display.*;
import flash.events.*;
import todd.interactive.ButtonSet;
public class DisablingButton extends MovieClip {
var labels:Array;
var thisParent:*;
var thisIndex:int;
public function DisablingButton() {
labels = this.currentLabels;
this.addEventListener(MouseEvent.CLICK, disableButton);
this.addEventListener(MouseEvent.ROLL_OVER, over);
this.addEventListener(MouseEvent.ROLL_OUT, out);
this.addEventListener(Event.ADDED,setParent);
}
function disableButton(event:MouseEvent):void {
for (var i:int = 0; i < labels.length; i ) {
if (labels[i].name == "disable") {
this.gotoAndPlay("disable");
}
}
this.removeEventListener(MouseEvent.CLICK, disableButton);
this.removeEventListener(MouseEvent.ROLL_OVER, over);
this.removeEventListener(MouseEvent.ROLL_OUT, out);
enableOthers();
}
function enableButton():void {
this.addEventListener(MouseEvent.CLICK, disableButton);
this.addEventListener(MouseEvent.ROLL_OVER, over);
this.addEventListener(MouseEvent.ROLL_OUT, out);
this.gotoAndStop(1);
}
function over(event:MouseEvent):void {
for (var j:int = 0; j < labels.length; j ) {
if (labels[j].name == "over") {
this.gotoAndPlay("over");
}
}
}
function out(event:MouseEvent):void {
for (var k:int = 0; k < labels.length; k ) {
if (labels[k].name == "out") {
this.gotoAndPlay("out");
}
}
}
function setParent(event:Event):void {
if (this.parent is ButtonSet) {
thisParent=this.parent;
for (var w:int=0; w < thisParent.buttons.length; w ) {
if (this == thisParent.buttons[w]) {
thisIndex=w;
}
}
}
}
function enableOthers():void {
for (var z:int=0; z < thisParent.buttons.length; z ) {
if (z != thisIndex) {
thisParent.buttons[z].enableButton();
}
}
}
}
}
载入了两个常用类后,又载入了刚刚定义的ButtonSet类,这样我们就能使用ButtonSet的一些方法了。
注意:这个类必须继承Movieclip类,因为该类的对象是一个mc。
然后定义了一些全局变量(默认均为public)。
创建析构函数DisablingButton,labels = this.currentLabels; 这句话的意思是取得当前mc的label属性,以array的形式返回,包含了label.frame,label.name等等的属性。
然后监听自己的鼠标点击、移入、移出事件。
this.addEventListener(Event.ADDED,setParent);这句话的意思是当自己被添加进一个容器时调用setParent函数。
disableButton这个函数作用是,将当前mc的状态变成disabled,然后取消监听事件,同时激活其他的按钮。
enableButton函数的作用就是激活自己的监听事件,并初始化自己的状态。