当您单击该图标时,这将生成一个 MouseEvent。您可以通过以下两种方式之一处理此问题:您的图标可以将此一般事件转换为更具体地满足您自己的需求的自定义事件类型,并从图标的基类中调度该事件,或者您可以只在某个位置监听 MouseEvent更高的水平,然后在那里对它做出反应。
我将发布后一种方法的示例,但是因为我发现手动实例化和定位很无聊、重复并且通常很烦人,所以我将把它写成好像所有东西都实际放置在舞台上一样在 IDE 中:
public class Container extends Sprite {
protected var childApps:Vector.<Application> = new Vector.<Application>();
protected var _activeApp:Application;
public function Container() {
super();
for (var i:int=0; i < numChildren; i++) {
var childApp:Application = getChildAt(i) as Application;
if (childApp) {
childApps[childApps.length] = childApp;
childApp.addEventListener(MouseEvent.CLICK, setActiveApp);
}
}
}
protected function setActiveApp(e:Event):void {
_activeApp = e.currentTarget as Application;
}
public function get activeApp():void {
return _activeApp;
}
}
/****************************/
public class MainClass extends MovieClip {
public var container:Container;
public var growIcon:Icon;
public var shrinkIcon:Icon;
public function MainClass() {
super();
stop();
if (growIcon) {
growIcon.addEventListener(MouseEvent.CLICK, grow);
shrinkIcon.addEventListener(MouseEvent.CLICK, shrink);
}
}
protected function grow(e:MoudeEvent):void {
if (container && container.activeApp) {
var newScale = container.activeApp.scaleX * 1.5;
container.activeApp.scaleX = container.activeApp.scaleY = newScale;
}
}
protected function shrink(e:MoudeEvent):void {
if (container && container.activeApp) {
var newScale = container.activeApp.scaleX * .75;
container.activeApp.scaleX = container.activeApp.scaleY = newScale;
}
}
}
简而言之,尽可能避免直接与兄弟类对话,并将父级与子级之间的交互限制为暴露的公共属性。公开尽可能少的属性。