0 votes

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()
in Engine by (12 points)

1 Answer

0 votes

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 onspellbodyentered(), 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 :)

by (399 points)

OMG THANK YOU, MY GAME WORKS NOW

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

Your function setspelldirection(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

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

No problem at all! :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.