0 votes

Hello, I am creating a simple 2d Platform Game,
My problem is that in one of my animations, the bullet/Bone needs to be created after the animation is complete. Right now it creates the Bone and starts the animation at the same time.

I have tried putting the creation of the Bone projectile under onanimation_complete, but then it will only allow me to shot if my character is facing the left (direction given by key press "a"). Is there something I'm missing?

Here is the code if it helps.
Thanks!

extends KinematicBody2D

const WALK = 40
const SPRINT = 70
const GRAVITY = 10
const JUMP = -250
const FLOOR = Vector2(0, -1)

const BONE = preload("res://Bone.tscn")

var is_attacking = false

var velocity = Vector2()
var on_ground = true

func _physics_process(delta):
    if Input.is_action_pressed("D"):
        if is_attacking == false:
            velocity.x = WALK 
            $Player_Animation.play("Player_Run")
            $Player_Animation.flip_h = true
            if sign($Position2D.position.x) == -1:
                $Position2D.position.x *= -1

    elif Input.is_action_pressed("A"):
        if is_attacking == false:
            velocity.x = -WALK 
            $Player_Animation.play("Player_Run") 
            $Player_Animation.flip_h = false
            if sign($Position2D.position.x) == 1:
                $Position2D.position.x *= -1

    else:
        velocity.x = 0
        if on_ground == true && is_attacking == false:
            $Player_Animation.play("Player_Idle")

    if Input.is_action_just_pressed("Space"):
        if is_attacking == false:
            if on_ground == true:
                velocity.y = JUMP
                on_ground = false

    if Input.is_action_just_pressed( "ui_focus_next") && is_attacking == false:

        is_attacking = true
        $Player_Animation.play("Player_Attack")
        var bone = BONE.instance()
        if sign($Position2D.position.x) == 1:
            bone.set_bone_direction(-1)
        else:
            bone.set_bone_direction(1)
        get_parent().add_child(bone)
        bone.position = $Position2D.global_position

    velocity.y = velocity.y + GRAVITY

    if is_on_floor():
        on_ground = true
    else:
        if is_attacking == false:
            on_ground = false
            if velocity.y < 0:
                $Player_Animation.play("Player_Jump")
            else:
                $Player_Animation.play("Player_Fall")

    velocity = move_and_slide(velocity, FLOOR)



func _on_Sprite_animation_finished():
    is_attacking = false
in Engine by (12 points)

Is there a way for you to use the _on_Sprite_animation_finished() function to spawn the bullet? In other words, you add the code for spawning the bullet to the _on_Sprite_animation_finished() function?

1 Answer

0 votes

You could use the animation player to animate a Bool in the script. Then the fire function can check that the bool is true or false; firing when needed.

After the shot has been fired the script resets the bool.

Hope this helps.

by (1,480 points)
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 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 webmaster@godotengine.org with your username.