jeudi 15 septembre 2016

How to create smooth motion for a mouse follower along a predefined path?

I want to make a tracing game. I want my circle to follow the path as the user traces the letter (path of the letter). The user can not go back to the area which is already traced

    import flash.events.Event;
    import flash.geom.Point;

    var i: Number;
    var size: int = 80;
    var down: Boolean = false;
    var up: Boolean = true;
    var inside: Boolean = true;
    var outside: Boolean = true;
    var circle: Shape = new Shape();

    stage.addEventListener(Event.ENTER_FRAME, loop);
    stage.addEventListener(MouseEvent.MOUSE_UP, mouseup);
    char.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);

    function loop(e: Event) {
if (down == true) {
    // Checks if mouse pointer is on path i.e 'S' alphabet
    if (s.hitTestPoint(stage.mouseX, stage.mouseY, true)) {
        inside = true;
        outside = true;

        var point: Point = maskobj.globalToLocal(new Point(stage.mouseX, stage.mouseY));
        var point2: Point = new Point();

        //Checks if mouse pointer is completely outside of drawn area
        for (i = 0; i < 2 * Math.PI; i += (2 * Math.PI) / 10) {
            point2.x = stage.mouseX + (size / 3) * Math.cos(i);
            point2.y = stage.mouseY + (size / 3) * Math.sin(i);
            if ((maskobj.hitTestPoint(point2.x, point2.y, true))) {
                outside = false;
                break;
            }
        }

        //Checks if mouse pointer is completely inside drawn area
        for (i = 0; i < 2 * Math.PI; i += (2 * Math.PI) / 10) {
            point2.x = stage.mouseX + (size / 3) * Math.cos(i);
            point2.y = stage.mouseY + (size / 3) * Math.sin(i);

            if (!(maskobj.hitTestPoint(point2.x, point2.y, true))) {
                inside = false;
                break;
            }
        }

        //Character will be moved only if mouse position not to far from current position
        if (outside == false) {
            if (inside == false) {
                //Increases drawn area by drawing a circle shape in 'maskobj' MovieClip

                circle.graphics.beginFill(0x0000ff);
                circle.graphics.drawCircle(point.x, point.y, size);
                circle.graphics.endFill();
                maskobj.addChild(circle);

                //Moves character to new position
                char.x = stage.mouseX;
                char.y = stage.mouseY;
            }
        }
    }
}
    }

    function mouseup(e: MouseEvent): void {
up = true;
down = false;
    }

    function mousedown(e: MouseEvent): void {
down = true;
up = false;
    }

enter image description here enter image description here

When I trace the path,the motion is not smooth. Can someone please suggest a way to make the motion smooth OR suggest another way to achieve the same. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire