在flex中开发应用程序,加载动作脚本文件。
<DrawingArea id="drawingArea" xmlns="*" width="100%" height="100%" add="drawingArea_addHandler(event)"/>
我需要动态添加它,如何做到这一点?指导我
更新
这是我的绘图区域如何创建var da:DrawingArea=new DrawingArea
如何访问监听器功能?
公共函数DrawingArea()
{
超级();
addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void {
erase();
});
addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void {
x1 = mouseX;
y1 = mouseY;
isDrawing = true;
});
addEventListener(MouseEvent.MOUSE_MOVE, function(event:MouseEvent):void {
if (!event.buttonDown)
{
isDrawing = false;
}
x2 = mouseX;
y2 = mouseY;
if (isDrawing)
{
graphics.lineStyle(2, drawColor);
graphics.moveTo(x1, y1);
graphics.lineTo(x2, y2);
x1 = x2;
y1 = y2;
}
});
addEventListener(MouseEvent.MOUSE_UP, function(event:MouseEvent):void {
isDrawing = false;
});
}
答案 0 :(得分:0)
您将无法在内存中加载文件,然后在Acionscript中执行它,如果这是您想要的。但是,您可以根据某些事件(例如用户单击或特定的超时时间)在运行时创建DrawingArea
(或任何其他元素)。
答案 1 :(得分:0)
在这种情况下,您的函数是内联定义的。直到你不给他们名字,无论如何都不可能。 我会建议
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownFunction);
public function mouseDownFunction(event:MouseEvent = null ):void {
x1 = mouseX;
y1 = mouseY;
isDrawing = true;
}
然后它们应该可以通过对象访问。 DrawingArea.mouseDownFunction()
答案 2 :(得分:0)
我对您的代码进行了更改。
package
{
import flash.events.MouseEvent;
import mx.core.UIComponent;
public class DrawingArea
{
private var _target:UIComponent;
private var _x1:Number;
private var _y1:Number;
private var _x2:Number;
private var _y2:Number;
[Bindable]
private var _isDrawing:Boolean = false;
private var _drawColor:uint = 0xFFFF00
public function DrawingArea(target:UIComponent)
{
//TODO: implement function
_target = target
_target.addEventListener(MouseEvent.MOUSE_DOWN, downEvent, false, 0, true);
_target.addEventListener(MouseEvent.MOUSE_UP, upEvent, false, 0, true);
_target.addEventListener(MouseEvent.MOUSE_OUT, upEvent, false, 0, true);
}
private function downEvent(event:MouseEvent):void
{
_x1 = event.localX;
_y1 = event.localY;
_isDrawing = true;
_target.addEventListener(MouseEvent.MOUSE_MOVE, moveEvent, false, 0, true);
}
private function moveEvent(event:MouseEvent):void
{
if (!event.buttonDown)
{
_isDrawing = false;
}
_x2 = event.localX;
_y2 = event.localY;
if (_isDrawing)
{
_target.graphics.lineStyle(2, _drawColor);
_target.graphics.moveTo(_x1, _y1);
_target.graphics.lineTo(_x2, _y2);
_x1 = _x2;
_y1 = _y2;
}
}
private function upEvent(event:MouseEvent):void
{
_isDrawing = false;
_target.removeEventListener(MouseEvent.MOUSE_MOVE, moveEvent);
}
public function set drawColor(value:uint): void{
_drawColor = value;
}
}
}
创建了一个类DrawingAreaView.mxml,其中为DrawingArea创建了对象,并将类本身作为容器传递。
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns="components.*" xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="onInit();">
<mx:Script>
<![CDATA[
import components.DrawingArea;
private var drawingArea:DrawingArea;
private function onInit():void {
drawingArea = new DrawingArea(this);
}
]]>
</mx:Script>
</mx:Canvas>
在Application中使用上面的类。
<DrawingAreaView id="drawingArea" width="100%" height="100%"/>
or
private var _drawingAreaView:DrawingAreaView = new DrawingAreaView();
parentObject.addChild(_drawingAreaView);