DRAW a line while selecting.

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

How to draw the lines when selecting the objects like in the image " connect all the monsters ".

You could have a an _input() function which checks for when the user has touched and held their touch on the screen. Then the _input() function builds an array of points based upon where the user has been touching the screen. Finally, a Line2D is created based upon this array. Btw, a Line2D is created from a PoolVector2Array.

Ertain | 2019-06-20 16:30

Thanks but can you explain it in detail with a code or example.

THE HELIX | 2019-06-20 17:44

Sorry, it’s a bit difficult for me to throw something together. I think it would look something like this:

  1. Apply an Area2D to each of the sprites.

  2. Detect when the player touches and drags on the screen in the _input() function (you’ll need to use the InputEventScreenTouch class here).

  3. When they drag their finger over the Area2D, have it fire off a mouse_entered() signal. If I recall, this signal will detect when the user touches within the Area2D. Add the position of the sprite to an array which will be used to draw the line.

  4. In a _draw() function, have a Line2D (this is a link, btw) draw the line through those points. You’ll have to assign that array with all of the sprite positions to a PoolVector2Array, though.

  5. If the player moves their finger, the line will have to be redrawn. So you’ll need to call update() in the _process() function, i.e.

func _process():
    if finger_has_lifted:
         update()

Hope this puts you on the right track.

Ertain | 2019-06-22 02:43