My Try at the challenge

I took another approach to communicate across of the scripts, by using signals, not something we’ve learned yet, but I like to do research on topics, and signals were something I fell over.

Also, I’m using the Godot 3.1 Alpha 2, just to try out the new functionality in 3.1, which is why you will see that the declaration code is a little different, for example:

const SPEED : float = 750.0

Where the “: float” means that I am telling Godot that it shouldn’t infer what type the variable is, but instead, it should treat it as a float.

Player.gs

extends KinematicBody2D

var motion : Vector2 = Vector2()

const SPEED : float = 750.0
const GRAVITY : float = 3500.0
const JUMP_SPEED : float = -1500.0
const UP : Vector2 = Vector2(0, -1)

signal is_running_right
signal is_running_left
signal is_idle
signal is_jumping

func should_move_right():
	return Input.is_action_pressed("ui_right")

func should_move_left():
	return Input.is_action_pressed("ui_left")
	
func should_jump():
	return Input.is_action_pressed("ui_up")

func run():
	if should_move_right() and not should_move_left():
		motion.x = SPEED
		if is_on_floor():
			emit_signal("is_running_right")
	elif should_move_left() and not should_move_right():
		motion.x = -SPEED
		if is_on_floor():
			emit_signal("is_running_left")
	else:
		motion.x = 0
		if is_on_floor():
			emit_signal("is_idle")
	
func jump():
	if should_jump() and is_on_floor():
		motion.y = JUMP_SPEED
		emit_signal("is_jumping")

func fall(delta):
	if not is_on_floor():
		motion.y += GRAVITY * delta
	else:
		motion.y = 0

func update_motion(delta):
	fall(delta)
	run()
	jump()	
	move_and_slide(motion, UP)

func _physics_process(delta):
	update_motion(delta)

PlayerAnimation.gd

extends AnimatedSprite

onready var player = get_parent()

func _ready():
	player.connect("is_running_left", self, "_play_run_left")
	player.connect("is_running_right", self, "_play_run_right")
	player.connect("is_idle", self, "_play_idle")
	player.connect("is_jumping", self, "_play_jump")

func _play_run_left():
	flip_h = true
	play("run")
	
func _play_run_right():
	flip_h = false
	play("run")
	
func _play_idle():
	flip_h = false
	play("idle")
	
func _play_jump():
	play("jump")

Privacy & Terms