Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | yemi-jump |
For some reason my projectile only is only shot when my player is facing left, why is that?
Player.gd:
extends KinematicBody2D
const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -250
const FLOOR = Vector2(0, -1)
const FIREBALL = preload("res://Fireball.tscn")
var velocity = Vector2() # takes 2 values (x,y)
var on_ground = false
func _physics_process(delta): # happens every fram
if Input.is_action_pressed("ui_right"):
velocity.x = SPEED
$AnimatedSprite.play("run")
$AnimatedSprite.flip_h = false
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1
elif Input.is_action_pressed("ui_left"):
velocity.x = -SPEED
$AnimatedSprite.play("run")
$AnimatedSprite.flip_h = true
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1
else:
velocity.x = 0
if on_ground == true:
$AnimatedSprite.play("idle")
if Input.is_action_pressed("ui_up"):
if on_ground == true:
velocity.y = JUMP_POWER
on_ground = false
if Input.is_action_just_pressed("ui_focus_next"):
var fireball = FIREBALL.instance()
if sign($Position2D.position.x) == 1:
fireball.set_fireball_direction(1)
else:
fireball.set_fireball_direction(-1)
get_parent().add_child(fireball)
fireball.position = $Position2D.global_position
velocity.y += GRAVITY
if is_on_floor():
on_ground = true
else:
on_ground = false
if velocity.y < 0:
$AnimatedSprite.play("jump")
else:
$AnimatedSprite.play("fall")
velocity = move_and_slide(velocity, FLOOR)
Fireball.gd:
extends Area2D
const SPEED = 150
var velocity = Vector2()
var direction = 1
func _ready():
pass # Replace with function body.
func set_fireball_direction(dir):
direction = dir
if dir == -1:
$AnimatedSprite.flip_h = true
func _physics_process(delta):
velocity.x = SPEED * delta * direction # translate doesn't multipy delta automaticaly
translate(velocity)
$AnimatedSprite.play("shoot")
func _on_VisibilityNotifier2D_screen_exited():
queue_free() # object no longer exists
What does the sign() function do?
Jowan-Spooner | 2019-06-09 12:32
it is a signal from Positon2D
yemi-jump | 2019-06-09 15:20
sign is the signum function. From the dicumentation:
float sign( float s ): Returns the sign of s: -1 or 1. Returns 0 if s is 0.
sign(-6) # returns -1
sign(0) # returns 0
sign(6) # returns 1
Kaligule | 2019-06-10 14:55
Thanks for making it clear!
Jowan-Spooner | 2019-06-11 16:40