0

I am a beginner in as3 and 've been following a simple flash sketchpad script, here's the link > http://www.sitepoint.com/create-flash-sketchpad/ . It's coded in as2 but I want to convert the codes to as3.I 've been trying to convert them to as3 but I just can't seem to make it work. I would deeply appreciate it someone could help me.

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

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

onMouseUp=function() {
    onMouseMove=null; 
}
1
  • Share what you tried as well, & we will help you correct it. Commented Jan 3, 2015 at 13:15

2 Answers 2

2

I decided to create an entire working example, so you can understand how this works;

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");
        }

    }

}

Hopefully this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to understand this now.Thanks a lot for the help.
@p ppt - no problem. If you don't quite know why I declared some of the code inside init(), it's just to make sure that the class has access to a reference to the stage when the class adds event listeners and invokes addchild(); You'll find other topics on using an init function.
1

This will give in 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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.