What is not working with my instanced Area2D and Collision2D nodes?

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

Hi!

I have a structured scene set like this:

- Root node (Position2D)
-- Area2D                    <-- Is an instance called by script
--- CollisionShape2DA <--- Is also an instance, set as Area2D child by script
----- SpriteA
--- CollisionShape2DB <--- Is also an instance, set as Area2D child by script
----- SpriteB 

I have this as the Area2D script:

extends Area2D

func _input_event(viewport, event, shape_idx):
 if event is InputEventMouseButton \
    and event.button_index == BUTTON_LEFT \
    and event.is_pressed():
        self.on_click()

func on_click():
   print("Click")

However, when I click on the CollisionShape, nothing is printed (While this works fine in a scene where Area2D and CollisionShape2D aren’t instances but already set nodes).
What am I doing wrong?

How do you instanciate them?

RenenerG | 2019-02-23 09:09

I instance the Area2D with:

var ThePlan = Position2D.new()
add_child(ThePlan)
ThePlan.set_name('ThePlan')
var TheArealoaded = load("res://TheArea.tscn")
var TheArea = TheArealoaded.instance()
ThePlan.add_child(TheArea)
TheArea.set_name("TheArea")

And each child is instanced by:

var obj = load(mypath)
var objinstance = obj.instance()
$ThePlan/TheArea.add_child(objinstance)

Capture
Here is a capture of the “remote inspector” (I don’t know how it’s called) showing that the scene is set up right when I launch it. I can also see that the script is attached to TheArea, however when I click on Handle1Area, it doesn’t print “click”.

Elis | 2019-02-23 10:30

Okay, I’ve solved it. The problem was that the root node of the scene where I instanced all of that was a Canvas. It blocked TheArea.
Does it mean that if I want a background, I should use a Sprite rather than a Canvas? Or maybe I just didn’t understand how to use a Canvas…

Elis | 2019-02-23 10:55