June 2018
Beginner to intermediate
298 pages
7h 38m
English
Attach a new script to the Player node. Add the following code to create the player's state machine:
extends KinematicBody2Denum {IDLE, RUN, JUMP, HURT, DEAD}var statevar animvar new_animfunc ready(): change_state(IDLE) func change_state(new_state): state = new_state match state: IDLE: new_anim = 'idle' RUN: new_anim = 'run' HURT: new_anim = 'hurt' JUMP: new_anim = 'jump_up' DEAD: hide() func _physics_process(delta): if new_anim != anim: anim = new_anim $AnimationPlayer.play(anim)
In the Inspector, set the run_speed, jump_speed, and gravity to values of 150, -300, and 750 respectively. Once again, you're using enum to list the allowed states for the system. When you want to change the player's state, you'll call change_state() ...
Read now
Unlock full access