+1 vote

I have a Line2D with a texture that tiles depending on the length of the line.

I would like to animate this line using some frames I have saved as individual png files. I don't know the best way to go about it.

I tried using an AnimationPlayer which loads new textures into the Line2D, but it was grossly inefficient to load a new texture every few milliseconds.

Any ideas about how to animate a Line2D?

in Engine by (1,604 points)

I wonder why this was flagged.

Hmm... me too?

1 Answer

+2 votes

You probably want to make a sprite sheet (put all frames on the same image), load it as an AtlasTexture and update its region property with an AnimationPlayer.

I haven't tryed this but seems like the way to go about it. Let us know how it goes ;)

by (76 points)

Alright, I have created a spritesheet with two frames called cord.png, which has a resolution of 62x12. Each frame is 31x12. I added the following code to my _ready()method in the hopes that it would set the Line2D's texture as the first frame of the spritesheet.

# load atlas texture
atlas_texture = AtlasTexture.new()
atlas_texture.atlas = load("img/cord.png")
atlas_texture.region = Rect2(Vector2(0,0), Vector2(31,12))
get_node("Line2D").texture = atlas_texture

But the Line2D still seems to use the entire spritesheet as its texture, rather than the subregion of it.

Any thoughts about what I'm doing wrong?

It appears the above method works fine for a Sprite but not for a Line2D. I wonder if Line2Ds are simply unable to use an AtlasTexture as their texture.

Yes, I did some testing and unfortunately it seems it completely ignores the Region property, picks up the hole image and renders it (in a tiled fashion) once and never again. I presume for optimization purposes. In fact I've been tinkering with lines recently and, once created, the image doesn't update even when you change the line's points from script.

I am also interested in this functionality (an updating Line2D) for my current project although I wasn't going to get into it until a couple more weeks, but still, I'm going to look into making a plugin for it. I'll let you know if I manage to get something working but for the time being it seems like there's no solution to this problem.

Best of lucks ;)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.