0
\$\begingroup\$

I'm working with monogame, moving a sprite object up and down. I want to draw a trail from the sprite's position and scrolling off to the left of the screen - like the sprite was a pencil leaving marks on a long sheet of paper sliding underneath it.

Here is an animated example:

Animated example of a heart sprite drawing a line behind it

I know how to move the sprite up and down but I'm a bit lost on how to do the trailing part. If someone could point me in the right direction I would really appreciate it

\$\endgroup\$
  • \$\begingroup\$ Could you reupload the image to stack's imgur account using the 'image' tool in the edit bar? This would prevent link rot in case pastebord.co shuts down or decide to remove the image. \$\endgroup\$ – Vaillancourt Jan 25 '17 at 12:44
1
\$\begingroup\$

A point, in the right direction?

You should have an array for points that will be drawn on the screen. And every frame, you should be capturing the position of the player and adding that to that array. Then, iterate through the array to add points to the screen. To reduce lag, you can also delete points from the array that have left the screen.

e.g.:

// The following code will not run properly:

public Vector2 playerPosition;

// Assume some code is here

public Vector2[] pointPositions;

public void Update()
{
    for(int i = 0; i < pointPositions.length; i++)
    {
        // Assuming your positions are in pixels
        pointPositions[i].X = pointPositions[i].X - 1;
    }
    pointPositions += playerPosition;
}

public void Draw()
{
    for(int i = 0; i < pointPositions.length; i++)
    {
        // Assuming you have a drawPoint(Vector2) function:
        drawPoint(pointPositions[i]);
    }
}
\$\endgroup\$
  • \$\begingroup\$ The heart is moving up and down only, not left to right, so at each frame, each of the points should be moved one pixel left. \$\endgroup\$ – Vaillancourt Jan 25 '17 at 17:21
  • \$\begingroup\$ Yeah, that too. I didn't think about that. \$\endgroup\$ – user96776 Jan 25 '17 at 19:02
  • \$\begingroup\$ Thanks for the answer. In fact I actually did that before you posted. Works like a charm. I will accept this as the answer \$\endgroup\$ – Eminem Jan 26 '17 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.