Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | haiderali |
I have created a scene with an area2d node, collision shape and a sprite. I want to check for mouse click on each spawned node in the level 1 but it is not working. Although when I run it individually just single scene, clicking by mouse on the node works fine. Why it is not working when I spawn it by creating new instances.
extends Node2D
var red_circle = preload("res://Scenes/RCircle.tscn")
var blue_circle = preload("res://Scenes/BCircle.tscn")
onready var line = $Line2D
onready var box_container = $BoxContainer
var circle_counter = 0
var circles = []
func _ready():
init_circle_list()
$ColorRect.set_size(get_viewport().get_visible_rect().size)
func init_circle_list():
for i in range(get_viewport().get_visible_rect().size.x - 50):
for j in range(get_viewport().get_visible_rect().size.y - 50):
if( (j+50) % 120 == 0 and (i+50) % 120 == 0):
circle_counter += 1
var p = red_circle.instance() if (circle_counter % 2 == 0) else blue_circle.instance()
p.position = Vector2(i,j)
circles.append(p)
box_container.add_child(circles[-1])
print(circles.size())
And the code for the Circles is
extends Area2D
func _input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
print("you clicked at blue")
Post your code so we can see if there are any errors.
timothybrentwood | 2021-12-11 20:02
Hi!
thank you for your response.
I have updated the question and uploaded my code please check it.
Thank you
haiderali | 2021-12-12 04:52
Remove or comment out this line:
$ColorRect.set_size(get_viewport().get_visible_rect().size)
then see if it works.
timothybrentwood | 2021-12-12 14:09
OMG
It worked by removing the ColorRect Node which i was using as a background.
Please suggest me how to set a background image in an appropriate way. I think sprite would be a good choice to set it as a background image.
Thank you soooooo much
haiderali | 2021-12-12 16:40