0 votes

I added two Position2D(s) in my project and I want that when the character is facing right($Sprite.flip_h == false) the bullet moves toward the right and if the character is facing left ($Sprite.flip_h == true) the bullet direction is left. The problem is that it is not shooting while playing idle. This is the script that I am using.

func _physics_process(delta):
    if Input.is_action_just_pressed("ui_focus_next"):
            var fireball = FFIREBALL.instance()
            if Input.is_action_pressed("ui_right"):
                get_parent().add_child(fireball)
                fireball.set_fireball_direction(1)
                fireball.global_position = $Position2DRight.global_position
            elif Input.is_action_pressed("ui_left"):
                get_parent().add_child(fireball)
                fireball.global_position = $Position2DLeft.global_position
                fireball.set_fireball_direction(-1)
            elif $Sprite.flip_h == false:
                fireball.set_fireball_direction(1)
            elif $Sprite.flip_h == true:
                fireball.set_fireball_direction(-1)

and this is the code for Bullet

extends Area2D

const SPEED = 500
var velocity = Vector2()
var direction = 1

func set_fireball_direction(dir):
    direction = dir
    if dir == -1:
        $BulletSprite.flip_h = true

func _physics_process(delta):
    velocity.x = SPEED * delta * direction
    translate(velocity)
    $BulletSprite.play("shoot")

func _on_VisibilityNotifier2D_screen_exited():
    queue_free()
in Engine by (12 points)

1 Answer

+1 vote

The "idle" code doesn't attach the bullet to the player.

Remove

get_parent().add_child(fireball)

from the left and right if statements, and place it right after instantiation:

var fireball = FFIREBALL.instance()
get_parent().add_child(fireball)
by (343 points)

I did it and now the fireball is working when idle is playing. When the player is moving it shoots in the position of Position2DRight or Position2DLeft but when it is playing idle the fireball is running through the upper side of the screen.

Add

fireball.global_position = $Position2DRight.global_position

and

fireball.global_position = $Position2DLeft.global_position

to the "idle" if statements.

My code for idle is

else:
        motion.x = 0
        $Sprite.play("idle")

And when inserted fireball.globalposition = $Position2DRight.globalposition and
fireball.globalposition = $Position2DLeft.globalposition it says Identifier 'fireball' is not decleared in the current scope. After adding var and getparent().addchild(fireball) to my code, it look like this and it is not showing any erorrs

else:
        motion.x = 0
        $Sprite.play("idle")
        var fireball = FFIREBALL.instance()
        get_parent().add_child(fireball)
        fireball.global_position = $Position2DRight.global_position
        fireball.global_position = $Position2DLeft.global_position

Now it keeps shooting while playing idle. If I press enter(which is key for shooting), the fireball appeares on the top of screen.

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.