Create a Line from the Last Object Created to Player

Hello,

The last major feature for my game that I haven’t been able to figure out yet is how to make a line that follows my ball and can stick around afterwords.

It’s simple enough to draw a line from one object to another, but what I’m hoping to do is a little more complex. Basically: Every time my ball stops or bounces off another object, I need to start a new line while keeping the old one. That new line connects the previous stop or bounce to the player until a new stop or bounce is registered.

I’ve set it up so far that each time I stop or bounce, a Target object is created. However, I don’t know how to link them together. How would I say that a line must connect from Target1 to Ball, but then when a bounce happens, connect the line from Target1 to Target2 and start a new line from Target2 to Ball?

Thanks!

EDIT: I have since partially solved the issue. I have a line that draws each time the ball stops and hits an object now. However, it REALLY does not like physics so far and will duplicate lines constantly when it touches a physics object. Eventually it will cause slowdown and if you end up drawing a ton of lines, it will crash the game. I’ll keep working on it, but I’m still open to suggestions if you have any.

Can you share a screenshot with Ball and targets and lines? :slight_smile:
It’ll be easier to understand.
If you draw tons of lines, you forgot to put a “Trigger once” somewhere.

@Gruk An image should be attached now.

I tried adding Trigger Once to the drawing command, but that causes no lines to be drawn at all. Most likely because of the way I’m drawing them.

I understand what is happening now - basically, if it hits a physics object that isn’t specifically mentioned in the code already to drop a new line target, it will continuously draw a new line from the previous target to the ball. Since the physics object didn’t cause it to go straight when compared to the last target, you get a ton of lines that often form a solid wall.

I should be able to fix it by adding a collision check for every single obstacle I make… Seems like there should be a better way though.

ExtraLines

Thanks for the image, captions might have helped. :sweat_smile:
I’ll assume the small circle on the left is your ball. It looks like you keep drawing lines from the ball to that red dot while the ball moves, so it’s very likely that you’re missing a Trigger once somewhere.
Please share a screenshot of all the events related to the line drawing.

@Gruk Fair enough, that would help. Thanks for checking in again.

Yes, the circle on the left is the ball and I draw lines from the last red dot (LineTarget) to the ball. I have it looking every frame to draw the line so that it stays continuous, and on the line’s object itself, I have “Clear the rendered image between frames” turned off so that the lines stick around after they are drawn.

The image shows how I’m drawing the line at the bottom. The top is an example of how I generate a new target every time the ball hits an object. The yellow X is where I’ve tried “trigger once” but if I put it there, no lines draw at all anymore because it’s not checking constantly to see if the ball moved away from the last target.

If I add a condition for every single obstacle, it will fix most of the lines for me. The only thing that I think would still be a problem would be when the ball rolls on a “hill” because that does not generate new targets for the line.

Move the create linetarget above chande id

Your event is always running, so you keep drawing a new line, so you do need to clear the previous lines to avoid having a wall of lines. :slight_smile:

@Slash I tried moving LineTarget above Change ID but it doesn’t quite work. that won’t draw lines anymore unfortunately.

@Gruk That is correct. I tested it by setting opacity to 10% and noticed that it is drawing a new line overtop of the old one every frame, which means that tons of extra lines are being drawn. I need to figure out a way so that every time it adds a new line, it removes the previous line UNLESS a new LineTarget has been created, because I need the last line to stay where it is. It should end up being one line rather than hundreds though… Not sure how to do this yet, but I’ll try to figure it out soon.

I figured out how to make it work with my physics objects now. Basically I just have it create new targets every frame, which gives it a smooth curve too. Looks nice! I does create a lot of tiny objects, but unlike with the lines themselves that need to stay, I can set targets to delete themselves whenever they leave the screen.

There’s only one bug left related to targets now, which is that my wall physics very occasionally do not create a target. It’s rare, but when that happens I also get odd streaks of lines. However, I think this will mostly be solved if I figure out how to delete old lines as well.

1 Like

Wouldn’t it be easier to use a line object and create/rotate/resize/delete them as you like?

@Gruk
I’m not sure what you mean. I am creating a line. I’m not sure how I’d use an object as a line.

I meant to use a line.png as a tiled sprite object.

@Gruk it might be better because then I would be able to add some style to the line… but I don’t know how to do that at all. Is there a tutorial somewhere for that?

I’ve been away from the game for a bit now but I’m not really any close to solving it yet unfortunately. The lines I draw now are fine but they do draw every frame so it causes problems. Maybe I can try getting them to clear each frame until it’s close to an object or something.

Well, create a tiled sprite object, create its animation in Piskel, change sprite size to 100 px per 3 px. Fill desired color with the bucket tool. Save sprite.
Then use the actions to create, position, rotate and resize the lines as you like.