저는 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))에 이벤트를 추가하려고 시도했지만 해당 메서드가 실행되지 않습니다.
문제점 :
감사합니다. 실제로 그래픽 API를 사용하여 백그라운드에서 일부 셰이프를 그립니다 (이 셰이프와 상호 작용할 필요는 없습니다). 그러나 'onMouseDown'메서드에서 볼 수 있듯이 Spark 프리미티브 rect를 만들고이를 'addElement'를 사용하는 패널 ... – jroy
Ah. 네가 지금하고있는 일을 볼 수있다. 리스너를 요소로 추가하기 전에 플레이어에 각 단서에 대한 별도의 표시 객체를 갖게하는 "단서"를 제공하십시오 (Rects는 마우스 이벤트를 수신 할 수 없음). 그래도 작동하지 않으면 MXML을 통해 모든 작업을 수행 할 수 있도록 내가 제안한대로 DataGroup을 사용해보십시오. –
OK Rects가 마우스 이벤트를 수신 할 수 없다는 것을 알지 못했습니다. 나는 이전에 청취자를 추가하려고 시도했지만 작동하지 않았습니다 ... 귀하 또는 tousdan이 제안한 것을 고맙게 생각합니다. 감사합니다. – jroy