CHAIN matching .

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

I am creating a game like in the image.How can I select the same color bubbles and draw a line between the selected bubbles like in the image ?

[Here is the link of the game video created in unity]
enter image description here

:bust_in_silhouette: Reply From: Dlean Jeans

Since you don’t have a base code or anything, I’m just gonna whip up some pseudo code here and you can go figure out the detailed implementation.

Let’s call those tiles Hex which are of type TouchScreenButton. Turn on Passby Press.

var hex_list = []

func _ready():

func _connect_hexes_to_signals():
  for hex in get_all_hexes():
    hex.connect('pressed', self, '_on_hex_pressed', [hex])
    hex.connect('released', self, '_on_hex_released', [hex])

func _on_hex_pressed(hex):
  if hex_list.empty() or hex.color == hex.front().color

func _on_hex_released(hex):
  if mouse_is_on(hex):
    hex_list = []

Things you can easily figure out yourself:

  • Use Line2D to draw connections with update_line2d
  • Write mouse_is_on to check if mouse or touch is on a Hex

For the bubble I used kinematicbody 2D, is it ok ?

THE HELIX | 2019-06-29 18:07

Yes, but you need something to detect mouse/touch input, like a TouchScreenButton added as a child.

Dlean Jeans | 2019-06-30 03:37

When executing I get the error
" Method ‘get_all_hexes’ is not declared in the current class."

THE HELIX | 2019-06-30 11:34

Oh yeah, you gotta write that one yourself. If they’re all under a node, just replace it with $ParentNode.get_children(). In the worst case, add the Hex scene to a group then call get_tree().get_nodes_in_group('Hexes').

Dlean Jeans | 2019-06-30 12:00

What is " mouse_is_on " ?

THE HELIX | 2019-07-02 03:34

It’s a function you write to check whether the mouse is on the tile. You can connect the input_event signal of the KinematicBody2D to a function to check and store it in a variable, I guess.

Dlean Jeans | 2019-07-02 03:56

I tried your code it didn’t work.Maybe i didn’t understand your code.

THE HELIX | 2019-07-02 12:13

But thanks for your help.your code is right but it is my problem.

THE HELIX | 2019-07-03 14:43