未在自定义Flex组件中触发的事件

时间:2011-06-24 07:00:34

标签: flash flex events flex4 custom-component

我正在通过扩展UIComponent类来编写自定义flex 4组件。不幸的是,我无法让组件响应任何类型的鼠标事件。我已经尝试将mouseEnabled设置为true是组件,以及在父级(舞台对象)中将mouseChildren设置为true。

无论我做什么,我的点击事件都可以从舞台上检测到,但不能用于组件。

这是我的组件类:

package components {

    import mx.core.UIComponent;

    public class DrawCanvas extends UIComponent {

        public function DrawCanvas() {
            super();
        }
    }
}

这是我的WindowedApplication文件:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       xmlns:cmp="components.*"
                       minWidth="800" minHeight="600"
                       applicationComplete="init()">

    <fx:Script>
        <![CDATA[
            private function init():void {
                myBox.addEventListener(MouseEvent.CLICK, reportClick);
                stage.addEventListener(MouseEvent.CLICK, stageClick);
            }

            private function stageClick(event:MouseEvent):void {
                trace(event.target, event.currentTarget);
                trace("Stage Click", event.localX, event.localY);
            }

            private function reportClick(event:MouseEvent):void {
                trace(event.target, event.currentTarget);
                trace("Click", event.localX, event.localY);
            }
        ]]>
    </fx:Script>

        <cmp:DrawCanvas id="myBox"
                    height="100%" width="100%"/>

</s:WindowedApplication>

提前致谢,

萨姆

1 个答案:

答案 0 :(得分:2)

组件只能通过可见部件调度鼠标事件。只要您的组件没有任何内容,它就无法触发鼠标事件。尝试类似:

package components {

    import mx.core.UIComponent;

    public class DrawCanvas extends UIComponent {

        public function DrawCanvas() {
            super();
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            graphics.clear();
            graphics.beginFill(0xFFFFFF, 0);
            graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
            graphics.endFill();
        }
    }
}