0 votes

I have 2 scenes: the main scene and an Area2D scene. In my main scene I've created instances of the Area2D as shown below.

func ready():
var k = area2D.instance()
for i in range (10):
var s = k.duplicate()
add
child(s)
listplayers.pushback(s)
listplayers[i].setposition(Vector2((150 * i),357))

func _input(event):

if list_players[0]._input(event):
    $Label.text = "one"
    print("one")

elif list_players[1]._input(event):
    print("two")
    $Label.text = "two"

... and so on

My Area2D has the following code:


func _input(event):
if event is InputEventMouseButton:
return true

I've been stuck with this for a long time now, I've done a lot of googling but I'm still really confused. I've tried making script, sub-resources unique, didn't work.Every time I run the game, 'one' gets printed out 4 times (even though I've made 6 instances) no matter what I click on. I want the Area2D event to be run only when a certain event is clicked and the label to display corresponding text. What can I do? Please reply asap, this is urgent

in Engine by (12 points)

Not sure what you're trying to achieve. Do you have two different Area2Ds and want to respond to being clicked on? If so, it's simple:

extends Area2D

func _ready():
    connect("input_event", self, "_on_input_event")

func _on_input_event(viewport, event, shape_idx):
    if event is InputEventMouseButton and event.pressed:
        print("Hello from " + name)

Add two area 2d in your scene, each with this script attach. Give them different names. You'll see that clicking on one of them doesn't trigger the other (unless they're overlapping).

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.