2 yinluhui yinluhui 于 2013.06.04 13:55 提问

flex 自定义组件事件问题

实现功能:定义自定义组件中mx:Image控件的鼠标经过事件:
自定义组件(名称为Component1.mxml)主要代码:

    <fx:Script>
private function mouseover(event:MouseEvent):void {
                //分发(广播)事件;当点击登录按钮时就触发了这一事件
                var e:MyEvent= new MyEvent(MyEvent.myMOUSEOVER);
                this.dispatchEvent(e);
            }
            private function mouseout(event:MouseEvent):void {
                //分发(广播)事件;当点击登录按钮时就触发了这一事件
                this.dispatchEvent(new MyEvent(MyEvent.myMOUSEOUT));
            }
        ]]>
    </fx:Script>
    <fx:Metadata>
        <!--add event-->
        [Event(name="myMouseover",type="bridge.MyEvent")]
        [Event(name="myMouseout",type="bridge.MyEvent")]
    </fx:Metadata>
<mx:Image id="imgShouZhan" mouseOver="mouseover(event)" mouseOut="mouseout(event)" height="10" width="10" right="2" bottom="2" scaleContent="true" name="imgShouZhan"/>

添加类:

package bridge {
    import flash.events.Event;

    public class MyEvent extends FlexEvent {
        public static const myMOUSEOVER:String="myMouseover";
        public static const myMOUSEOUT:String="myMouseout";    

        public function MyEvent(type:String) {
            super(type, false, false);
        }
    }
}

定义监听事件:

var compTemp:Component1= new Component1();
compTemp.GetImgShouZhan().addEventListener(MyEvent.myMOUSEOVER, SetImgShouZhanUrl);//GetImgShouZhan()方法返回<mx:Image>对象
private function SetImgShouZhanUrl(event:MyEvent):void {
    Alert.show("Preview");
}

上面的代码仍然无法监听到image控件的鼠标经过事件,请大神帮忙看看?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!