![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | corbybender |
I have a “Main” node and associated with some menu buttons, one of 2 methods gets called. SpawnBlue, SpawnRed.
I want to spawn multiple Blue items along a Path2D
-BluePath2D
—BluePathFollow2D
------BlueGuy *added as child in code below *
At top before ready() set this:
export var BlueGuyNode = preload("res:///Scenes/BlueGuy.tscn")
Then triggered by a button click, this gets called.
func SpawnBlueGuy():
var BlueGuy = BlueGuyNode.instance()
var blueGuyPath = get_node("BluePath2D/BluePathFollow2D")
blueGuyPath.offset = 0
blueGuyPath.add_child(BlueGuy)
pass
What I am trying to get happen is that when the button is clicked the first time, BlueGuy gets spawned and begins walking around his path. Let’s just say it is a circle and the starting position is 12 0’clock.
So next, let’s say when he is at the 4 0’clock position and I click the button again, a second BlueGuy should spawn at 12 o’clock
What is happening, though, is that when the button is clicked for the second time, BlueGuy #1 gets jumped back to the starting position of the circle and BlueGuy #2 gets added in the same position…so it looks like only 1 guy is there even though there are two.
And on the BlueGuy code, in ready() I am grabbing the BlueGuy path like this:
blueGuyPath = MainScene.get_node("BluePath2D/BluePathFollow2D")
and the movement is handled in _process like so:
func _process(delta: float) -> void:
blueGuyPath.offset += 50 * delta
How can I update this code to let BlueGuy #1 keep his current position when the button is clicked a second time and BlueGuy #2 gets added to the path (at start position)? This happens even if I click Add 20 times (…I put a counter to test whether or not there were indeed multiple BlueGuy items and there is)
2D scenes
Note that physics_process()
is the recommended way to handle movement, as it runs in a separate thread and should not be influenced by fps fluctuations.
skysphr | 2022-01-25 08:44