将简单脚本从actionscript 2转换为actionscript 3

时间:2015-01-03 13:08:46

标签: actionscript-3 flash actionscript-2

我是as3的初学者,并且一直在关注简单的Flash草图脚本,这里的链接是> http://www.sitepoint.com/create-flash-sketchpad/。它在as2中编码,但我想将代码转换为as3。我一直试图将它们转换为as3,但我似乎无法使其正常工作。我很感激有人可以帮助我。

createEmptyMovieClip("Line",1);
Line.lineStyle(3,0x000000,300);

onMouseDown = function () {
    Line.moveTo(_xmouse, _ymouse);
    onMouseMove = function () {
        Line.lineTo(_xmouse, _ymouse);
    } 
}

onMouseUp=function() {
    onMouseMove=null; 
}

2 个答案:

答案 0 :(得分:2)

我决定创建一个完整的工作示例,以便您了解其工作原理;

package {
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    /**
     * ...
     * @author Martyn
     */
    public class Main extends Sprite {

        public var draw:Shape = new Shape();

        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

            trace("init");
            addChild(draw);
            draw.graphics.lineStyle(3, 0x000000, 300);

        }
        private function onMouseDown(e:MouseEvent):void
        {
           stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
           draw.graphics.moveTo(mouseX, mouseY);
           trace("onMouseDown");
        }

        private function onMouseUp(e:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            trace("onMouseUp");
        }

        private function onMouseMove(e:MouseEvent):void
        {
            draw.graphics.lineTo(mouseX, mouseY);
            trace("onMouseMove");
        }

    }

}

希望这有帮助。

答案 1 :(得分:1)

这将在AS3中给出:

var Line:Sprite = new Sprite();
addChild(Line);
Line.graphics.lineStyle(3, 0x000000, 1);

stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

function mouseDownHandler(e:MouseEvent):void {
    Line.graphics.moveTo(e.stageX, e.stageY);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}

function mouseMoveHandler(e:MouseEvent):void {
    Line.graphics.lineTo(e.stageX, e.stageY);
}

function mouseUpHandler(e:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}