How to change Animated sprite on trigger Event?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Smit

Here I have created a player(Area2D) and there is a stone here with RigidBody2D on it, I have made a signal which gets emitted when the player enter or steps on the stone , What i want to do is when it steps on the stone i want to change the animation of the player into a animation with him having a stone in his hand

:bust_in_silhouette: Reply From: SnapCracklins

This depends on implementation. Are you going to change the whole sprite to a new file that has the stone in the animation or are you going to have a stone sprite appear on a position of the player sprite? I’ve seen it both ways. Either way, a sprite update is easy.

extends Sprite

var _nextSprite = load("insert_sprite_path_here")

func _spriteChange():
		$Sprite.texture = _nextSprite

Then just plug that function in on your rock pickup.

What about animationSprite ?? I have an Animation sprite called walk_has_stone which is basically the same walk sprite but i gave stone in his hand and made a animation out of it … how would i change the animation on trigger … because on emit_signal i am using $ben/animatedSprite.animation = “name of the animation” but its not working also ben is the name of the file .

`func on_step():



Smit | 2022-11-27 08:12

Oh that’s pretty easy. However, with AnimatedSprite, it pulls from a resource and not a texture. So you can just cycle through different SpriteFrames resources, just make sure all the names of animations match. So for instance, you could have two sets of SpriteFrames, one called say “BareHanded” and one called “RockEquipped” and have animations with the same names but different images. Like “walk” and “run” and “attack” that way you don’t have to change code for animation when you change frames. So instead of .texture use .frames.

So like:

$AnimatedSprite.frames = load("res://new_spriteframes1.tres")

SnapCracklins | 2022-11-27 17:16