0

我正在开发加载动作脚本文件的 flex 应用程序。

<DrawingArea id="drawingArea"   xmlns="*" width="100%" height="100%" add="drawingArea_addHandler(event)"/>

我需要动态添加它,怎么做?指导我

更新 这是我的绘图区如何创建var da:DrawingArea=new 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;
        });
    }
4

3 回答 3

0

如果这是您想要的,您将无法在内存中加载文件然后在 Acionscript 中执行它。但是,您可以做的是DrawingArea在运行时基于某些事件(例如用户单击或特定超时时间)创建一个(或任何其他元素)。

于 2012-04-24T09:46:12.297 回答
0

在这种情况下,您的函数是内联定义的。除非你不给他们名字,否则无论如何都是不可能的。我会建议

addEventListener(MouseEvent.MOUSE_DOWN, mouseDownFunction);
public function mouseDownFunction(event:MouseEvent = null ):void {
        x1 = mouseX;
        y1 = mouseY;
        isDrawing = true;
    }

然后它们应该可以通过对象访问。DrawingArea.mouseDownFunction()

于 2012-04-24T11:08:44.603 回答
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>

在应用程序中使用上述类。

<DrawingAreaView id="drawingArea" width="100%" height="100%"/>

or

private var _drawingAreaView:DrawingAreaView = new DrawingAreaView();

parentObject.addChild(_drawingAreaView); 
于 2012-04-24T12:25:38.863 回答