2011-10-17 51 views
0

我对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)),但相应的方法从未执行...

问题是什么?

回答

0

使用图形API绘制的对象不是事件派发器。因此,使用addElement添加您正在创建但不以任何方式(对容器如Group)而不是使用图形api绘制形状的Spark基元rects。

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

+0

谢谢你的建议!实际上,我使用图形API在后台绘制一些图形(我不需要与这些图形进行交互),但正如您在'onMouseDown'方法中所见,我创建了Spark原始图形并将其添加到面板使用'addElement'... – jroy

+0

啊。我明白你现在在做什么。尝试添加侦听器_before_将其添加为元素,以便为玩家提供一个“线索”,您希望为每个Rect单独显示对象(Rects不能单独接收鼠标事件)。如果这不起作用,请按照我的建议尝试使用DataGroup,以便通过MXML完成所有工作。 –

+0

好吧我只是不知道Rects无法接收鼠标事件。我试图添加侦听器,但它没有奏效......我想我会尝试你或tousdan的建议,谢谢。 – jroy

0

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