Edit: Thanks for the help everyone, however something is still wrong with the player as it would seem. It still calls the timer twice playing the hurt animation twice as well. Also whenever I begin the world scene, it first plays the invulnerable code. I don't know if I read the instructions you guys gave me wrong, or if something in the code is messed up. Anyway, any help would be appreciated as I continue to work on this. : )
Here is the updated code
extends KinematicBody2D
var movement = Vector2()
var up = Vector2(0,-1)
var isAttacking = false
var facing_right = true
var dead = false
var hurt = false
var knockback = 300
const GRAVITY = 20
const ACCEL = 11
const MAXFALLSPEED = 250
const MAXSPEED = 130
const JUMPFORCE = 405
const NEWGRAVITY = 10
const MAXGLIDER = 150
func _process(delta):
if dead == false && hurt == false:
movement.y += GRAVITY
if movement.y > MAXFALLSPEED:
movement.y = MAXFALLSPEED
if facing_right == true:
$AnimatedSprite.scale.x = 1
else:
$AnimatedSprite.scale.x = -1
movement.x = clamp (movement.x, -MAXSPEED, MAXSPEED)
if Input.is_action_pressed("ui_right"):
get_node("HitBox").set_scale(Vector2(1,1))
elif Input.is_action_pressed("ui_left"):
get_node("HitBox").set_scale(Vector2(-1,1))
if Input.is_action_pressed("ui_right") &&isAttacking == false:
movement.x += ACCEL
facing_right = true
if is_on_floor() == true:
$AnimatedSprite.play("Move")
elif Input.is_action_pressed("ui_left") && isAttacking == false:
facing_right = false
movement.x -= ACCEL
if is_on_floor() == true:
$AnimatedSprite.play("Move")
else:
movement.x = lerp (movement.x, 0, 0.2)
if isAttacking ==false && is_on_floor() == true:
$AnimatedSprite.play("Idle")
if is_on_floor() && isAttacking == false:
if Input.is_action_just_pressed("ui_up"):
movement.y = -JUMPFORCE
if !is_on_floor():
if movement.y < 0 :
$AnimatedSprite.play("Jump")
elif movement.y > 0:
$AnimatedSprite.play("Fall")
if Input.is_action_pressed("Attack") && is_on_floor() && hurt == false:
$AnimatedSprite.play("Attack")
isAttacking = true
$HitBox/CollisionShape2D.disabled = false
if Input.is_action_pressed("Glide"):
if is_on_floor() == false:
$AnimatedSprite.play("Glide")
movement.y += NEWGRAVITY
if movement.y >= MAXGLIDER:
movement.y = MAXGLIDER
if Input.is_action_pressed("Restart"):
get_tree().reload_current_scene()
movement = move_and_slide(movement, up * delta )
func _on_AnimatedSprite_animation_finished():
if $AnimatedSprite.animation == "Attack":
$HitBox/CollisionShape2D.disabled = true
isAttacking = false
if $AnimatedSprite.animation == "Dead":
get_tree().reload_current_scene()
func _on_DeathBox_body_entered(body):
# hurt = true
if hurt == false:
modulate.a = 0.5
$AnimatedSprite.play("Hurt")
$Slime_Timer.start()
hurt = true
func _on_Slime_Timer_timeout():
hurt = false
$Slime_Timer2.start()
func _on_Slime_Timer2_timeout():
modulate.a = 1
Hey Everyone! I just started making my first game ( I know you have heard that before ) and I have hit a roadblock. When the player collides with the enemy it plays the hurt animation and then starts an invulnerable state. The problem is, that after it plays the hurt and invulnerable state, it plays it again. ( in about two seconds, which is probably something to do with the timer ) . Any help would be much appreciated as I work out this problem.
Below is the code:
extends KinematicBody2D
var movement = Vector2()
var up = Vector2(0,-1)
var isAttacking = false
var facing_right = true
var dead = false
var hurt = false
var knockback = 300
const GRAVITY = 20
const ACCEL = 11
const MAXFALLSPEED = 250
const MAXSPEED = 130
const JUMPFORCE = 405
const NEWGRAVITY = 10
const MAXGLIDER = 150
func _process(delta):
if dead == false && hurt == false:
movement.y += GRAVITY
if movement.y > MAXFALLSPEED:
movement.y = MAXFALLSPEED
if facing_right == true:
$AnimatedSprite.scale.x = 1
else:
$AnimatedSprite.scale.x = -1
movement.x = clamp (movement.x, -MAXSPEED, MAXSPEED)
if Input.is_action_pressed("ui_right"):
get_node("HitBox").set_scale(Vector2(1,1))
elif Input.is_action_pressed("ui_left"):
get_node("HitBox").set_scale(Vector2(-1,1))
if Input.is_action_pressed("ui_right") &&isAttacking == false:
movement.x += ACCEL
facing_right = true
if is_on_floor() == true:
$AnimatedSprite.play("Move")
elif Input.is_action_pressed("ui_left") && isAttacking == false:
facing_right = false
movement.x -= ACCEL
if is_on_floor() == true:
$AnimatedSprite.play("Move")
else:
movement.x = lerp (movement.x, 0, 0.2)
if isAttacking ==false && is_on_floor() == true:
$AnimatedSprite.play("Idle")
if is_on_floor() && isAttacking == false:
if Input.is_action_just_pressed("ui_up"):
movement.y = -JUMPFORCE
if !is_on_floor():
if movement.y < 0 :
$AnimatedSprite.play("Jump")
elif movement.y > 0:
$AnimatedSprite.play("Fall")
if Input.is_action_pressed("Attack") && is_on_floor() && hurt == false:
$AnimatedSprite.play("Attack")
isAttacking = true
$HitBox/CollisionShape2D.disabled = false
if Input.is_action_pressed("Glide"):
if is_on_floor() == false:
$AnimatedSprite.play("Glide")
movement.y += NEWGRAVITY
if movement.y >= MAXGLIDER:
movement.y = MAXGLIDER
if Input.is_action_pressed("Restart"):
get_tree().reload_current_scene()
movement = move_and_slide(movement, up * delta )
func _on_AnimatedSprite_animation_finished():
if $AnimatedSprite.animation == "Attack":
$HitBox/CollisionShape2D.disabled = true
isAttacking = false
if $AnimatedSprite.animation == "Dead":
get_tree().reload_current_scene()
func _on_DeathBox_body_entered(body):
hurt = true
if hurt == true:
modulate.a = 0.5
$AnimatedSprite.play("Hurt")
$Slime_Timer.start()
func _on_Slime_Timer_timeout():
hurt = false
$Slime_Timer2.start()
func _on_Slime_Timer2_timeout():
modulate.a = 1