The character is not shooting while playing idle

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

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()
:bust_in_silhouette: Reply From: sparkart

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)

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.

Muneeb | 2019-06-08 17:33

Add

fireball.global_position = $Position2DRight.global_position

and

fireball.global_position = $Position2DLeft.global_position

to the “idle” if statements.

sparkart | 2019-06-08 17:56

My code for idle is

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

And when inserted fireball.global_position = $Position2DRight.global_position and
fireball.global_position = $Position2DLeft.global_position it says Identifier ‘fireball’ is not decleared in the current scope. After adding var and get_parent().add_child(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.

	

Muneeb | 2019-06-08 19:10