Can I repeat a area2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By unknowndexy

Is it possible for me to make Area2D gets duplicated and spawns like 20 pixels forward the currently area2d and repeat that progress and so it spawns like 10 pixels higher and lower so its random?

:bust_in_silhouette: Reply From: jgodfrey

Sure, that can be done.

I might recommend something like:

  • Create a standalone scene containing the Area2D and anything else you want to be a part of your spawned instances.
  • In a script on some controlling scene, preload your Area2D scene using something like var area2d = preload("res://Scenes/Area2D.tscn")
  • Create a loop that will spawn as many instances as you want. You could use something like this to drive the loop for i in range(10): (that’ll execute the code in the loop 10 times)
  • In the loop, create a new instance of your preloaded scene using var newArea = area2d.instance()
  • Also in the loop, position the newly instanced scene at some random location based on logic of your choice. With your new, random x and y position selected, you can place the new item there using newArea.postion = Vector2(newX, newY)

That’s really about it. Just season to taste…