I have four scenes in my project: the main scene, the platform scene(area2d,collisionshape, position1 and 2, staticbody2d(collisionshape and sprite) ) , the missile scene(kinematicbody2d, collisionshape, sprite) and the spaceship scene(kinematicbody2d,collisionshape, sprite) . I have only two scripts in the project: one is attached to the root node of the main scene and to this script I connected the bodyentered() and bodyexited() signal from the platform scene, plus there is another function that randomize the positions of the vector, and the other script in the spaceship scene. The problem comes when I start the game and when I move the spaceship to the top, the platform instantiates missiles (when the spaceship gets close enough the platform creates missiles that at the moment do nothing) and at a certain point the game crashes and I get the errors: "body set shape disabled" and "body set shape as one way collision". I tried using the call_defferred() function to add a missile to each position but it didn't work. How can I solve this problem?
Code of MAIN SCENE:
extends Node
var boolean = false
var vecPositions
var randomPosition
var MissileInstance
var MissileScene = preload("res://scenes/Missile.tscn")
var random = RandomNumberGenerator.new()
func random_Position(array : Array):
random.randomize()
var randPosition = array[random.randi() % array.size()]
return randPosition
func _on_Platform_body_entered(body):
if body is KinematicBody2D:
boolean = true
while(boolean):
vecPositions = [$Platform/Position1,$Platform/Position2]
randomPosition = random_Position(vecPositions)
if randomPosition == $Platform/Position1:
MissileInstance = MissileScene.instance()
$Platform/Position1.add_child(MissileInstance)
elif randomPosition == $Platform/Position2:
MissileInstance = MissileScene.instance()
$Platform/Position2.add_child(MissileInstance)
yield(get_tree().create_timer(3),"timeout")
func _on_Platform_body_exited(body):
if body is KinematicBody2D:
boolean = false
Code of SPACESHIP SCENE:
extends KinematicBody2D
var target
var click = false
var dir
var speed = 200
var collision
func _input(event):
if event is InputEventMouseButton:
target = get_global_mouse_position()
click = true
func _physics_process(delta):
if click:
dir = position.direction_to(target)
collision = move_and_collide(dir * speed * delta)
if collision:
pass