How to use my mouse to control my 2D character's direction ?

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

I want to know how to make my 2D character (PLAYER) flip to left , right , up and down depends on the mouse cursor .

Example :

If I move my mouse cursor to left , my character sprite should flip to the left .
if I move my mouse cursor to right , my character sprite should flip to right .
and again if i move to up , it should play a new sprite(I will make a new sprite that lookin up top) .Also for down.

Please help me !!
I am new to godot .
***English is not my language so i might have some grammar or spelling mistakes . ***

:bust_in_silhouette: Reply From: Wakatta

Assuming that you’re using an animated sprite attach this GDscript

extends AnimatedSprite

func _input(event):
	if event is InputEventMouseMotion:
		if event.relative.x > 0:
			$AnimatedSprite.animation = "right"
			$AnimatedSprite.flip_h = false
		elif event.relative.x < 0:
			$AnimatedSprite.animation = "right"
			$AnimatedSprite.flip_h = true
		elif event.relative.y < 0:
			$AnimatedSprite.animation = "up"
			$AnimatedSprite.flip_v = false
		elif event.relative.y > 0:
			$AnimatedSprite.animation = "up"
			$AnimatedSprite.flip_v = true

func _process(delta):
	$AnimateSprite.play()

And for a regular sprite

extends Sprite

func _input(event):
    if event is InputEventMouseMotion:
        if event.relative.x > 0:
        	$Sprite.flip_h = false
        elif event.relative.x < 0:
        	Sprite.flip_h = true
        elif event.relative.y < 0:
        	$Sprite.flip_v = false
        elif event.relative.y > 0:
        	$Sprite.flip_v = true