So I'm having some difficulties connecting body_enter of a script-generated Area2d, to the main script. Basically, I have a function scripted to initialize a barrel (kinematicbody2d, sprite, area2D, collision and collision shape). Would the problem be that a node cannot be connected at run time? Or maybe I'm not creating a CollisionShape2D correctly? If anyone could enlightenment me on these issues, I'd be very happy!
func _spawnBarrel():
var kine = KinematicBody2D.new()
kine.set_name("barrelStart")
var a2d= Area2D.new()
a2d.set_name("barrelCollision")
var col2d = CollisionShape2D.new()
var rec2d = RectangleShape2D.new()
rec2d.set_extents(Vector2(10,10))
col2d.set_shape(rec2d)
col2d.set_name("barrelCollisionShape")
a2d.add_child(col2d)
var sprite = get_node("spr_barrel")
add_child(kine)
kine.add_child(a2d)
remove_child(sprite)
a2d.add_child(sprite)
kine.set_pos(Vector2(320,0))
sprite.show()
get_node("barrelStart/barrelCollision).connect("body_enter",self,"barrel_entered")
func barrel_entered( body ):
print("IT HAS BEEN ENTERED INTO")