Hi again! The pain is only beginning... For now I did a little script for automatically instance a formation of babosos (invaders) in a Node2D. It works well and moves equally to the right in the beginning. Now, I added a script to the babosos for detecting that the border of screen is near and emit in consequence a signal to the parent (the Node2D that creates the formation and moves them) that calls a function who order moves down the formation. The problem is that the instanced invaders seem that cannot detect the global size of the screen and never emits the signal. It looks like they take the position of the Node2D as his "0, 0" coordinate and, because is moving continuosly, never detect his global position in the screen.
The question is, ¿can a instanced scene be aware of her global position in the screen instead of the relative position off his parent?
This is the provisional formation creator and mover...
extends Node2D
export (PackedScene) var Baboso_Basic
const formation = Vector2 ( 6, 3)
var velocity = Vector2()
var speed = 20
var descent_lenght = 16
var next_height = 0
signal need_new_height
func _ready():
position = Vector2( 0, 0)
for y in range (formation.y):
var baboso1Pos = Vector2( 0, (y * 20) + 24) #El +16 es para separar del borde superior
for x in range (formation.x):
baboso1Pos.x = (x * 24) + 24 #El +16 es para separar del lateral izquierdo
var baboso1 = Baboso_Basic.instance()
baboso1.position = baboso1Pos
add_child(baboso1)
self.connect("need_new_height", self, "_add_new_height", [], CONNECT_ONESHOT)
get_node("Space_baboso_basic").connect("border_reached", self, "_on_baboso_reach_border")
_move_right()
func _on_baboso_reach_border(): #Move down when baboso reach border
velocity = Vector2( 0, 1)
func _move_right():
velocity = Vector2( 2, 0)
func _move_left():
velocity = Vector2( -2, 0)
func _add_new_height():
next_height = (self.position.y + descent_lenght)
func _physics_process(delta):
position += delta * (velocity * speed)
# if position.y == next_height:
# _move_left()
And this is the little code of the invader baboso...
extends KinematicBody2D
signal border_reached
func _ready():
add_to_group("total_babosos")
$AnimationPlayer.play("Idle")
func _process(delta):
self.position.x = clamp( position.x, 20, 300)
if self.position.x == 20 or self.position.x == 300:
emit_signal("border_reached")
print("baboso has reached the border!")
Well, at least I learned how tho post the code embebed into the post :)