Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Richardv07 |
I’m having a bit of trouble with getting a sprite to follow (or appear on) a PathFollow2D when I create the PathFollow2D via script.
func _ready():
path2d = Path2D.new()
curve = path2d.curve
#Add curve points to create a planetary orbit
curve.add_point ( Vector2 (0, yrad), Vector2( 0, 0 ), Vector2( xrad, 0 ), -1 )
curve.add_point ( Vector2 (xrad, 0), Vector2( 0, 0 ), Vector2( 0, -yrad ), -1 )
curve.add_point ( Vector2 (0, -yrad), Vector2( 0, 0 ), Vector2( -xrad, -0 ), -1 )
curve.add_point ( Vector2 (-xrad, 0), Vector2( 0, 0 ), Vector2( 0, yrad ), -1 )
curve.add_point ( Vector2 (0, yrad), Vector2( 0, 0 ), Vector2( 0, 0 ), -1 )
#Add a new PathFollow2D as a child of the Path2D
path2d.add_child(PathFollow2D.new())
var followpath = path2d.get_child(0)
#Create a test sprite and set its texture
var testsprite = Sprite.new()
testsprite.set_texture(preload("res://StarlightGalaxy/Materials/Planets/Asteroid1.png"))
#Add testsprite to the FollowPath2D
followpath.add_child(testsprite)
followpath.set_offset(0)
#Draw orbit of planet
func _draw():
draw_polyline(curve.get_baked_points(), Color(1,1,1,.1), 2.0)
func _process(delta):
followpath.set_offset(followpath.get_offset() + 50 * delta)
The above is a simplified version of what I’m trying to do (my actual script generates multiple orbits, then pulls from sprite factories etc) but even the above isn’t working for me.
If I print the name of “testsprite” it gives me Sprite 12312 or whatever random number it generated, as opposed to “Null,” but there is no sprite visible in-game (as far as I can see). I see the drawn Path2D, but not the child “testsprite.”
I also deleted the background in the scene to ensure the sprite isn’t simply appearing beneath the background.
If I haven’t given enough info, let me know and I can add more. Thank you in advance!