#I need help with this

1 messages · Page 1 of 1 (latest)

vague badger
#

extends CharacterBody2D

var enemy_inattack_range = false
var enemy_attack_cooldown = true
var health = 160
var player_alive = true

const speed = 100
var current_dir = "none"

func _ready():
$AnimatedSprite2D.play("front_idel")

func _physics_process(delta):
player_movement(delta)
enemy_attack()

if health <= 0:
    player_alive = false
    print("player has died!")
    self.queue_free()

func player_movement(_delta):
if Input.is_action_pressed("ui_right"):
current_dir = "right"
play_anim(1)
velocity.x = speed
velocity.y = 0
elif Input.is_action_pressed("ui_left"):
current_dir = "left"
play_anim(1)
velocity.x = -speed
velocity.y = 0
elif Input.is_action_pressed("ui_down"):
current_dir = "down"
play_anim(1)
velocity.y = speed
velocity.x = 0
elif Input.is_action_pressed("ui_up"):
current_dir = "up"
play_anim(1)
velocity.y = -speed
velocity.x = 0
else:
play_anim(0)
velocity.x = 0
velocity.y = 0

func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D
if dir == "right":
anim.flip_h = false
if movement == 1:
anim.play("right_run")
elif movement == 0:
anim.play("right_idel")
if dir == "left":
anim.flip_h = true
if movement == 1:
anim.play("right_run")
elif movement == 0:
anim.play("right_idel")
if dir == "down":
anim.flip_h = true
if movement == 1:
anim.play("front_run")
elif movement == 0:
anim.play("front_idel")
if dir == "up":
anim.flip_h = true
if movement == 1:
anim.play("back_run")
elif movement == 0:
anim.play("back_idel")

move_and_slide()
#

func player():
pass

func _on_player_hitbox_body_entered(body):
if body.has_method("enemy"):
enemy_inattack_range = true

func _on_player_hitbox_body_exited(body):
if body.has_method("enemy"):
enemy_inattack_range = false

func enemy_attack():
if enemy_inattack_range and enemy_attack_cooldown == true:
health = health - 20
enemy_attack_cooldown = false
$attack_cooldown.start()
print(health)

func _on_attack_cooldown_timeout():
enemy_attack_cooldown = true

#

Sorry if the code are unreadable

blissful nest
#

[] code

#

a help box should come up... use triple backward apostrophes to cite code on this server

#

are the pictures and code posts different scripts? the enemy_attack() code is different

vague badger
#

I delete it to see will it work that way but it didn't

lament pivot
#

But also code isn't a macro. You probably are thinking of []codeblock (or []cb)

blissful nest
#

ah ok, oops