Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Bubbles |
I can not get my character to shoot in both directions. It starts out by shooting to the left and when i move to the left it shoots right. so i have both directions but when i move to the right again it continues to shoot right?
This is my script for the Player!
extends KinematicBody2D
const SPEED = 150
const GRAVITY = 10
const JUMP_POWER = -450
const FLOOR = Vector2(0, -1)
const FIREBALL = preload(“res://Fireball.tscn”)
var velocity = Vector2()
var on_ground = false
func _physics_process(delta):
if Input.is_action_pressed("ui_right"):
velocity.x = SPEED
$Sprite.play("run")
$Sprite.flip_h = false
if sign($Position2D.position.x) == -1:
$Position2D.position.x = -1
elif Input.is_action_pressed("ui_left"):
velocity.x = -SPEED
$Sprite.play("run")
$Sprite.flip_h = true
if sign($Position2D.position.x) == 1:
$Position2D.position.x = 1
else:
velocity.x = 0
if on_ground == true:
$Sprite.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 $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:
$Sprite.play("jump")
else:
$Sprite.play("fall")
velocity = move_and_slide(velocity, FLOOR)
and this is my script for the fireball:
extends Area2D
const SPEED = 200
var velocity = Vector2()
var direction = 1
func _ready():
pass
func set_fireball_direction(dir):
direction = dir
if direction == -1:
$AnimatedSprite.flip_h = true
func _physics_process(delta):
velocity.x = SPEED * delta * direction
translate(velocity)
$AnimatedSprite.play("shoot")
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
do anyone know what i do wrong?
Hi,
First of all, can you correctly format your code? like this:
extends KinematicBody2D
const SPEED = 150
const GRAVITY = 10
const JUMP_POWER = -450
const FLOOR = Vector2(0, -1)
const FIREBALL = preload("res://Fireball.tscn")
var velocity = Vector2()
var on_ground = false
func _physics_process(delta):
if Input.is_action_pressed("ui_right"):
velocity.x = SPEED
$Sprite.play("run")
$Sprite.flip_h = false
if sign($Position2D.position.x) == -1:
$Position2D.position.x = -1
elif Input.is_action_pressed("ui_left"):
velocity.x = -SPEED
$Sprite.play("run")
$Sprite.flip_h = true
if sign($Position2D.position.x) == 1:
$Position2D.position.x = 1
else:
velocity.x = 0
if on_ground == true:
$Sprite.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()
fireball.set_fireball_direction($Position2D.position.x )
else:
fireball.set_fireball_direction(-1)
get_parent().add_child(fireball)
fireball.position = $Position2D.global_position
velocity.y += GRAVITY
if isonfloor():
on_ground = true
else:
on_ground = false
if velocity.y < 0:
$Sprite.play("jump")
else:
$Sprite.play("fall")
velocity = move_and_slide(velocity, FLOOR)
Second, you dont need to if something == true:
, you can simply ask if something:
And third, can you share me the project so i can do some test? I cannot see a bug in the code you shared… maybe testing would be easier. And also, where you are updating $Position2D.position
? because that is a local position relative to its parent, it won’t change when parents position change i think. And couldn’t you use the state of flip_h
to set direction instead of position
?
p7f | 2019-01-08 12:34
thank you for the pointers. will look in to them as soon as possible. of course i can share it to you but perhaps a stupid question, how do i do it?
Bubbles | 2019-01-08 23:20
You can share it with google drive, mega, or any cloud storage and post the link here. Also, you could create a repo in github or similar ans share the link to it.
p7f | 2019-01-08 23:24
Thank you again for helping my here is the link to my project on google drive:
Bubbles | 2019-01-09 07:53
Have you by any chans had some time over to look on my projekt to see what could be wrong with it?
Bubbles | 2019-01-15 14:20
My hard drive broke, and i o my cell now… ill replace it soon and have a look at it.
p7f | 2019-01-15 14:22
Sorry to hear about the harddrive of course and again Thanks alot for the help!
Bubbles | 2019-01-15 14:32
Hi, i borrowed my girlfriend’s notebook and i think i solved this. I’ll post it as an answer… Tell me if it works!
p7f | 2019-01-15 18:53