How to Reset a Path

I am trying to pass in random values into the shotPath function. Things work as expected on the first shot, but subsequent shots start from the end point of the previous path, and then things dance around.

  1. How do I reset it so it always generates a fresh path from the 540, 1550 coordinate?

  2. How can I clear the ball?

Destroy is not documented at all. There are no notes, but apparently this completely destroys it and you can’t use it again. I don’t see a rest or clear function. ball.stopFollow() does something, but not what I want.

    shotPath(finX, finY, curveWidth, curveHeight);

    ballpath = new Phaser.Curves.Path(540, 1550);
    ball = this.add.follower(ballpath, 540, 1550, 'ball');

    function shotPath(finX,finY, curveWidth, curveHeight) {
        ballpath.quadraticBezierTo(finX, finY, curveWidth, curveHeight);

        ball.startFollow({
            from: 0,
            positionOnPath: false,
            duration: 3000,
            repeat: 0
        });
    }

Side note: Pasting from WebStorm on macOS doesn’t work on to the forum, I have to paste in another window first.

Edit: It appears gets appended instead of replaced, so I am ended up with concatenated curves like this:

ballpath.quadraticBezierTo(finX, finY, curveWidth, curveHeight).quadraticBezierTo(finX, finY, curveWidth, curveHeight).quadraticBezierTo(finX, finY, curveWidth, curveHeight);

I am looking for something like delete ballpath.Curves[0];

With followers you can call setPath() again, replacing the old path with a new one.

With a path, you really could just create a new one. But if you want to reuse, I think you would do

path.curves.length = 0;
path.cacheLengths.length = 0;
3 Likes

Great success! Thank you.

I noticed some error when console logging about length and 0, but I wouldn’t have made this connection for a while. I spent about a good six hours trying to figure this out. I think this should go as an example because it is so common.

I don’t want to construct a new object because another function is calling this specific function to pass randomized variables to show a path from a single point. I want to avoid using update at all as the project I am doing now doesn’t need it. I also ran into a problem of a draw tracer path of the ball not being drawn until a second in. Followers don’t mix so nicely with drawing in some cases. Drawing is probably the way to do it for most things.