事件问题(未检测到)

时间:2011-10-17 15:10:03

标签: actionscript-3 flex

我是Flex和ActionScript的新手,我正在尝试创建一个应用程序,其目的只是为了能够在面板上动态创建矩形:在鼠标按下时,矩形创建并在鼠标移动事件上更新(左按钮按住),然后用鼠标向上实现矩形。

<?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"
                       width="550" height="400"
                       creationComplete="this.onCreationComplete(event);">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.graphics.SolidColor;

            import spark.primitives.Graphic;
            import spark.primitives.Rect;

            public static const WIDTH:int = 480;
            public static const HEIGHT:int = 300;
            public static const BACKGROUND:int = 0xEFEFEF;
            public static const CURRENT_FILL:SolidColor = new SolidColor(0x00AA00, 0.75);

            protected var rectangles:ArrayCollection = null;

            protected var currentRect:Rect = null;
            protected var dragging:Boolean = false;

            protected function onCreationComplete(event:FlexEvent):void
            {

                this.rectangles = new ArrayCollection();

                this.sprite.graphics.beginFill(BACKGROUND);
                this.sprite.graphics.drawRect(0, 0, this.sprite.width, this.sprite.height);
                this.sprite.graphics.endFill();

                this.sprite.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
                this.sprite.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);

            }

            protected function onMouseDown(event:MouseEvent):void
            {

                this.currentRect = new Rect();
                this.currentRect.y = 10;
                this.currentRect.height = this.sprite.height - (10 * 2);

                this.currentRect.x = event.localX;
                this.currentRect.width = 1;

                this.panel.addElement(this.currentRect);

                this.dragging = true;

                this.sprite.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);

            }

            protected function onMouseUp(event:MouseEvent):void
            {
                if (this.dragging)
                {

                    this.sprite.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);


                    this.rectangles.addItem(this.currentRect);
                    this.dragging = false;

                }   
            }

            protected function onMouseMove(event:MouseEvent):void
            {
                if (this.dragging)
                {
                    this.currentRect.width = event.localX - this.currentRect.x;     
                }
            }

        ]]>
    </fx:Script>

    <s:Panel id="panel" x="10" y="10" title="Calendar">
        <s:SpriteVisualElement id="sprite" width="{WIDTH}" height="{HEIGHT}" />
    </s:Panel>

</s:WindowedApplication>

一切正常,但现在我想在用户点击(或双击,或你想要的)矩形时做一些动作。我试图在每个矩形上添加一个事件(onMouseUp中添加了this.currentRect.addEventListener(MouseEvent.DOUBLE_CLICK,myMethod)),但相应的方法永远不会被执行...

有什么问题?

2 个答案:

答案 0 :(得分:0)

使用图形API绘制的对象不是事件调度程序。因此,使用addElement添加您正在创建但不以任何方式使用的Spark原语(对于诸如Group的容器),而不是使用图形api来绘制形状。

然而,您可能会发现,只需让ArrayCollection包含一堆可绑定数据对象并使用带有itemRenderers的DataGroup来显示数据就更好了。

答案 1 :(得分:0)

您可以跟踪您在集合中绘制的矩形,并在鼠标单击时使用矩形进行命中测试,并确定是否正在点击任何矩形。