add c# code to 2d-sprite-animation page

also, do not teach people to `get_node` every frame

(cherry picked from commit ba38d9dbc5)
This commit is contained in:
Paul Joannon
2020-12-04 01:01:55 +01:00
committed by Rémi Verschelde
parent d87fa5e941
commit efb87c31fb

View File

@@ -79,11 +79,37 @@ released.
extends KinematicBody2D
onready var _animated_sprite = $AnimatedSprite
func _process(delta):
if Input.is_action_pressed("ui_right"):
$AnimatedSprite.play("run")
_animated_sprite.play("run")
else:
$AnimatedSprite.stop()
_animated_sprite.stop()
.. code-tab:: csharp
public class Character : KinematicBody2D
{
private AnimatedSprite _animatedSprite;
public override void _Ready()
{
_animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
}
public override _Process(float delta)
{
if (Input.IsActionPressed("ui_right"))
{
_animatedSprite.Play("run");
}
else
{
_animatedSprite.Stop();
}
}
}
Sprite sheet with AnimatedSprite
@@ -192,12 +218,37 @@ released.
extends KinematicBody2D
onready var _animation_player = $AnimationPlayer
func _process(delta):
if Input.is_action_pressed("ui_right"):
$AnimationPlayer.play("walk")
_animation_player.play("walk")
else:
$AnimationPlayer.stop()
_animation_player.stop()
.. code-tab:: csharp
public class Character : KinematicBody2D
{
private AnimationPlayer _animationPlayer;
public override void _Ready()
{
_animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
}
public override void _Process(float delta)
{
if (Input.IsActionPressed("ui_right"))
{
_animationPlayer.Play("walk");
}
else
{
_animationPlayer.Stop();
}
}
}
.. note:: If updating both an animation and a separate property at once
(for example, a platformer may update the sprite's ``h_flip``/``v_flip``