Added injury animation for Player – LLODO


In this part, I will add more injury animation for Player and about enemies added in the previous part, this part is also similar to the previous part.

Animation is injured

More animations

In the character design, I haven’t added the injury animation so you will add it for me.

[Tạo 2D Platformer Game với Godot]  Part 15: Added player injury animation

Add me an animation called bithuong and you will add it yourself, but I don’t guide and review the knowledge.

Then turn off the loop.

Apply animation

Then as mentioned in the article, the way it works is no different from the previous part.

I will create a variable bithuong to check if it is normal or not, then when attacked by an enemy, bithuong = true, dangtancong = false so that if attacked by an enemy it will not attack anymore but run the injured animation and run the injured animation. And I also check more in other conditions.

After that, if the animation ends with injury, bithuong = false

var bithuong = false

Turn injured.

func _on_Hurtbox_area_entered(area):
    if area.name == "Hitbox" and area.get_parent().dangtancong == true:
        mau -= area.get_parent().satthuong
        dangtancong = false
        bithuong = true
        animation.play("bithuong")

If hit, dangtancong = false, bithuong = true and run the injured animation.

func _on_AnimatedSprite_animation_finished():
    if animation.animation == "tancong":
        dangtancong = false
    if animation.animation == "bithuong":
        bithuong = false

Check if injured animation ends then variable bithuong = false.

if !dangtancong and not bithuong:
    huong_dichuyen = Input.get_axis("ui_left","ui_right")
    if huong_dichuyen != 0:
        chuyendong.x = lerp(chuyendong.x,huong_dichuyen * tocdo,0.5)
        animation.play("chay")
        scale.x = scale.y * huong_dichuyen
    else:
        chuyendong.x = lerp(chuyendong.x,0,0.5)
        animation.play("dungyen")

    if is_on_floor():
        if Input.is_action_just_pressed("ui_accept") :
            chuyendong.y = -250
    else:
        animation.play("nhay")

Added a condition so that when injured, I can’t move until it’s over.

if Input.is_key_pressed(KEY_J) and is_on_floor():
    if not bithuong:
        dangtancong = true
        chuyendong.x = 0
        animation.play("tancong")

Checks when the attack key is pressed and not in the normal state when it attacks.

extends KinematicBody2D

var tocdo = 200
var trongluc = 10
var chuyendong = Vector2()
var huong_dichuyen = 1

onready var animation = $AnimatedSprite
var dangtancong = false
var bithuong = false

var mau = 20
var satthuong = 10

func _physics_process(delta):
    
    if !dangtancong and not bithuong:
        huong_dichuyen = Input.get_axis("ui_left","ui_right")
        if huong_dichuyen != 0:
            chuyendong.x = lerp(chuyendong.x,huong_dichuyen * tocdo,0.5)
            animation.play("chay")
            scale.x = scale.y * huong_dichuyen
        else:
            chuyendong.x = lerp(chuyendong.x,0,0.5)
            animation.play("dungyen")

        if is_on_floor():
            if Input.is_action_just_pressed("ui_accept") :
                chuyendong.y = -250
        else:
            animation.play("nhay")
    if Input.is_key_pressed(KEY_J) and is_on_floor():
        if not bithuong:
            dangtancong = true
            chuyendong.x = 0
            animation.play("tancong")
    
    chuyendong.y += trongluc 
    chuyendong.normalized()
    chuyendong = move_and_slide(chuyendong,Vector2.UP) 
    
    if mau <= 0:
        set_physics_process(false)
        animation.play("chet")
        $CollisionShape2D.disabled = true
func _on_AnimatedSprite_animation_finished():
    if animation.animation == "tancong":
        dangtancong = false
    if animation.animation == "bithuong":
        bithuong = false
func _on_AnimatedSprite_frame_changed():
    if animation.animation == "tancong":
        if animation.frame == 3:
            $Hitbox/CollisionShape2D.disabled = false
        else:
            $Hitbox/CollisionShape2D.disabled = true 

func _on_Hurtbox_area_entered(area):
    if area.name == "Hitbox" and area.get_parent().dangtancong == true:
        mau -= area.get_parent().satthuong
        dangtancong = false
        bithuong = true
        animation.play("bithuong")

And I have the full code of this part as above.

[Tạo 2D Platformer Game với Godot]  Part 15: Added injury animation for Player 16

And I have the final result.

Summary

So in this part, I showed you how to use injured animation for characters.



Link Hoc va de thi 2021

Chuyển đến thanh công cụ