Animation not working after Area2D of collosion with Enemy

Help me, please.

I have code bellow, Aniamtion Death not working after I control Player collisions with Enemy, I use Node Area2D not working, everything else Animation working normal (Move, Jump).

Thank you so much !!

did the scene get reloaded after 1.5 seconds?

it does looks like you have no boolean variable for controlling if it’s in death sequence, do nothing else
also the death function will only need to be exactly triggered once, so your flag should turn back to false value after it enters the if flag: branch

Yes, scene reload after 1.5 seconds.

I tried set flag = false when it enter in If flag: bracnh, it seems ainmation Death not working.

PS: var flag I set default false.

you will still need to disable all the other movement when it’s animating death

1 Like

var flag not fixed yet:

After fixed:

yes, because like i said, you will also need a variable for “is already dead”
so your whole code before this flag branch wont be accessed if it’s alrdy on death animation

copy for me your whole _physics_process method code, i will edit it
make sure to put it inside
image
preformatted text

1 Like
extends CharacterBody2D


const SPEED = 200.0
const JUMP_VELOCITY = -400.0

@onready var anim = get_node("AnimatedSprite2D");

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var flag = false

#func _process(_delta):
		#return;
			
func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		anim.play("Jump");
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		anim.play("Run");
		anim.scale.x = direction; # flip: lật
		velocity.x = direction * SPEED
	else:
		anim.play("Idle");
		velocity.x = move_toward(velocity.x, 0, SPEED)
	
	if flag:
		anim.play("Death")
		death()
		return
		
	move_and_slide()	
	pass
	
# Player death when collision Enemy			
func death():	
	await get_tree().create_timer(1.5).timeout
	reload()
	
# Handle collision enemy	
func _on_area_2d_body_entered(body):
	if body.name == "Bee":
		flag = true
	pass # Replace with function body.

func reload():
	get_tree().change_scene_to_file("res://Scene/Worlds/World_1.tscn")

Ye, It working

extends CharacterBody2D


const SPEED = 200.0
const JUMP_VELOCITY = -400.0

@onready var anim = get_node("AnimatedSprite2D");

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var flag = false
var is_dead=false

#func _process(_delta):
		#return;
			
func _physics_process(delta):
	if is_dead:
		return
	# Add the gravity.
	if not is_on_floor():
		anim.play("Jump");
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		anim.play("Run");
		anim.scale.x = direction; # flip: lật
		velocity.x = direction * SPEED
	else:
		anim.play("Idle");
		velocity.x = move_toward(velocity.x, 0, SPEED)
	
	if flag:
		flag=false
		is_dead=true
		anim.play("Death")
		death()
		return
		
	move_and_slide()	
	pass
	
# Player death when collision Enemy			
func death():	
	await get_tree().create_timer(1.5).timeout
	reload()
	
# Handle collision enemy	
func _on_area_2d_body_entered(body):
	if body.name == "Bee":
		flag = true
	pass # Replace with function body.

func reload():
	get_tree().change_scene_to_file("res://Scene/Worlds/World_1.tscn")

When I check flag = false, before I play animation other, it working

In spite of I no undertand, why check flag = false before play animation, I did return after play animation Death, function func _physics_process(delta):
must end.

remember _process and _physics_process triggered every frame
so there will no stopping it to go repeat from top of its code block again
unless you add false in this
set_physics_process

1 Like

Thank you so much, have a nice day !!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.