Im having trouble with my first game

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

my spell keeps deleting itself for some reason. this is my first godot game. in the game there is 2 players whoa re wizards and they can each shoot a spell, the first one to cast a spell at the other wizard wins. there are some walls mixed in the game scene, they just consist of a sprite,staticbody2d and a colisonpolygon2d. im trying to make it so that when the spell casts it dies when it hits the colider, but instead i think its dying beside the player. the worst part is that they still dont even self destruct when they hit the wall! im not sure how to fix this, and i cant really find anything that can help.

spell script:

extends Area2D

const speed =150
var velocity =Vector2()

onready var AnimationPlayer = $AnimationPlayer

var direction = 1

func set_spell_direction(dir):
	direction = dir
	if dir == -1:
		$Sprite.flip_h = true

func _physics_process(delta):
	velocity.x = speed * delta * direction
	translate(velocity)
	AnimationPlayer.play("Spell")

func _on_VisibilityNotifier2D_screen_exited():
	queue_free()


func _on_Spell_area_entered(area):
	queue_free()

my players script:

extends KinematicBody2D

const Acceleration = 500
const Max_speed = 150
const Friction = 400
const Spell = preload("res://Scenes/Spell.tscn")

var velocity = Vector2.ZERO
var can_cast = true

onready var AnimationPlayer = $AnimationPlayer
onready var spell_item_position = $Spell_Cast_Posistion

func _physics_process(delta):
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	input_vector = input_vector.normalized()
	
	if input_vector != Vector2.ZERO:
		if input_vector.x > 0:
			AnimationPlayer.play("Walk")
			$Sprite.flip_h = false
		else:
				AnimationPlayer.play("Walk")
				$Sprite.flip_h = true
		velocity = velocity.move_toward(input_vector * Max_speed, Acceleration * delta)
	else:
		AnimationPlayer.play("Idle")
		velocity = velocity.move_toward(Vector2.ZERO, Friction * delta)

	velocity = move_and_slide(velocity)

	if Input.is_action_pressed("ui_shoot"):
		Cast()
		AnimationPlayer.play("Cast_Spell")

func Cast():
		var SPELL = Spell.instance()
		get_parent().add_child(SPELL)
		SPELL.position = get_node("Spell_Cast_Posistion").global_position

func _on_Hitbox_area_entered(area):
	queue_free()
:bust_in_silhouette: Reply From: RedBlueCarrots

I think your issue might be that your Area2D is set to either only interact with other areas, rather than bodies, or that you’ve simply connected the wrong signal from the Area2d Node. Try connecting _on_spell_body_entered(), and see if that fixes it.

If that only fixes the wall issue, then I think you are right that the area is dying right when it spwans, as the frame in which the area enters a body, it will call the signals you have attached. This includes the frame it spawns.

If worst comes to worst, you could add a check in the spell script that the body you are colliding with is not the same one which cast the spell to begin with.

hope this helps, comment below if you want more details :slight_smile:

OMG THANK YOU, MY GAME WORKS NOW

Gamermatteo168 | 2020-08-06 02:16

i acctually do have 1 more question, how do i flip the projectile (spell) so it shoots where the player faces?

Gamermatteo168 | 2020-08-06 02:52

Your function set_spell_direction(dir) should work, however you don’t ever call it!

in the Cast() function in the player script, add at the bottom

if $Sprite.flip_h:
    SPELL.set_spell_direction(-1)
else
    SPELL.set_spell_direction(1)

I think that will work, let me know if it doesnt

RedBlueCarrots | 2020-08-06 03:40

It worked, i couldn’t have completed this game without you!

Gamermatteo168 | 2020-08-07 00:09

No problem at all! :slight_smile:

RedBlueCarrots | 2020-08-07 00:23