一个类应该被放在一个package里面,就像钱应该被放到钱包里一样。package后面定义的是该类的路径。然后一系列常用的类。 定义了一个全局变量buttons,在变量前面加一个public就可以了。这样就能在整个类中被访问到。 类名应该和文件名一样(区分大小写),然后定义一个同名函数,这个函数会在类被初始化时调用,就像php4的类一样。这里只是搭了个架子,没有具体内容。 然后定义了一个函数addButtons,它的作用就是将一些mc或者sprites放到自己的container里(addChild)。 还有一个类:DisablingButton,也是位于todd->interactive文件夹下,这也是这个案例的核心。对了,之前已经将RectButton的linkage里的baseClass设置为todd.interactive.DisablingButton。
代码稍微有点长,且听我细细道来
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(); } } } } }
|