3 Tween, AnimatedSprite, Area2D
Here we will create a frog for our game, The frog will know the presense of Player using area2D, and jump away from the player.
Tween is a Node which allow us to change a object using different settings and duration.
It allows us to animate objects in the game. We will use Tween to make the frog jump.
var tween = get_tree().create_tween()
var init_position = position;
var half_final = init_position + Vector2(direction * 60, -40)
var final = half_final + Vector2(direction * 60, 40)
tween.tween_property(self, "position", half_final, .25)
tween.tween_property(self, "position", final, .25)
tween.tween_callback(jump_completed)
We had already used AnimatedSprite, but this time, we change the used sprite using the play method.
We use area2d to detech the player, detech the direction of the player and jump away.
func _on_body_entered(body):
var flip = false
var direction = -1
if body.global_position.x < global_position.x:
flip = true
direction = 1
animated_sprite_2d.flip_h = flip
animated_sprite_2d.play("jump")