Thursday, March 28, 2019

17. AnimatedSprite

We use a set of images from the website OpenGameArt for different animations mentioned on this site.



We have a Node with child of Label and Node2D, which has its child an AnimatedSprite. The Node2D is there, so we may set its scale of 0.5, 0.5. Thus we do not have to change scale of the AnimatedSprite to reduce image sizes. We have to rename the nodes as 'node2d', 'anim_sprite' and 'label' and position the label and image so they can be viewed.


For the Animated Sprite we add two actions, Idle-Normal and Idle-Blinking after creating the new SpriteFrames. There are many other set of sequences, but they were not used. Below, we show the sequence for Idle-Blinking:



We add this script to Node.



extends Node

var anim_sprite
var label

func _ready():
    anim_sprite = get_node('node2d/anim_sprite')
    label = get_node("label")

func _process(delta):
    if Input.is_action_pressed('ui_left'):
        anim_sprite.play("idle_blinking")
        label.set_text(anim_sprite.animation)
    elif Input.is_action_pressed('ui_right'):
        anim_sprite.play("idle_normal")
        label.set_text(anim_sprite.animation)
 


We have this output for the blinking case (left arrow pressed):


No comments:

Post a Comment