【问题标题】:Event issues (not detected)事件问题(未检测到)
【发布时间】:2011-10-17 15:10:03
【问题描述】:

我是 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>

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

有什么问题?

【问题讨论】:

    标签: actionscript-3 apache-flex


    【解决方案1】:

    使用图形 API 绘制的对象不是事件调度器。因此,请使用 addElement 添加您正在创建但未以任何方式使用的 Spark 原始矩形(添加到诸如 Group 之类的容器中),而不是使用图形 api 来绘制形状。

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

    【讨论】:

    • 感谢您的建议!实际上我正在使用图形 API 在背景中绘制一些形状(我不需要与这些形状进行交互),但是正如您在我的“onMouseDown”方法中看到的那样,我正在创建 Spark 原始矩形并将其添加到使用“addElement”的面板...
    • 啊。我明白你现在在做什么。尝试在将监听器添加为元素之前添加监听器,以便为玩家提供一个“线索”,即您希望为每个矩形拥有单独的显示对象(矩形不能自己接收鼠标事件) .如果这不起作用,请尝试按照我的建议使用 DataGroup,以便您可以通过 MXML 完成所有操作。
    • 好吧,我只是不知道 Rects 不能接收鼠标事件。我之前尝试添加监听器,但没有成功...我想我会尝试你或 tousdan 的建议,谢谢。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多