Skip to content

Instantly share code, notes, and snippets.

@willnationsdev
Last active August 4, 2017 14:52
Show Gist options
  • Save willnationsdev/25645e0728952d4784896410af61dd6c to your computer and use it in GitHub Desktop.
Save willnationsdev/25645e0728952d4784896410af61dd6c to your computer and use it in GitHub Desktop.
Answering question 1
extends KinematicBody2D
var direction = Vector2()
var anim = ""
onready var animplayer = getNode("AnimationPlayer")
var velocity = Vector2()
var speed = 0
const TOP = Vector2(0, -1)
const DOWN = Vector2(0, 1)
const LEFT = Vector2(-1, 0)
const RIGHT = Vector2(1, 0)
const MAX_SPEED = 700
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
var new_anim = "idle"
var is_moving = Input.is_action_pressed("move_up") or Input.is_action_pressed("move_right") or Input.is_action_pressed("move_down") or Input.is_action_pressed("move_left")
direction = Vector2()
if is_moving:
speed = MAX_SPEED
if Input.is_action_pressed("move_up"):
new_anim = "Backwards"
direction += TOP
elif Input.is_action_pressed("move_down"):
new_anim = "Forward"
direction += DOWN
if Input.is_action_pressed("move_left"):
new_anim = "Left"
direction += LEFT
elif Input.is_action_pressed("move_right"):
new_anim = "Right"
direction += RIGHT
else:
new_anim = "idle"
speed = 0
if(new_anim != anim):
anim = new_anim
animplayer.play(anim)
velocity = speed * direction * delta
move(velocity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment