Simple Grid Movement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By FightNite
:warning: Old Version Published before Godot 3 was released.

I am trying to get simple grid movement but I am new and I don’t know what to do. I used this script for the Node2D to make a look of the grid.

extends Node2D

var cell_size = 64 # 16,32,64…

func _draw(): for y in range(0,600,cell_size): for x in
range(0,1024,cell_size):
draw_line(Vector2(x,y),Vector2(x,y+cell_size),Color(0,1,0),1.0)
draw_line(Vector2(x,y),Vector2(x+cell_size,y),Color(0,1,0),1.0)

The KinematicBody2D is has a simple sprite child node and the KinematicBody2D script is this,

extends KinematicBody2D

const MAX_SPEED = 4 # 1,2,4,8 var direct =
{“N”:Vector2(0,-1),“S”:Vector2(0,1),“W”:Vector2(-1,0),“O”:Vector2(1,0)}
var step = 0; var move = “stop” var play_pos = Vector2(0,0)

func _ready():
self.set_pos(Vector2(get_node(“…”).cell_size,get_node(“…”).cell_size))
set_fixed_process(true)

func step_move(key,dir,speed): if(Input.is_action_pressed(key) and
move == “stop”) or move == dir: move = dir; step = 1 play_pos =
direct[dir] if step == get_node(“…”).cell_size/speed: move =
“stop”; step = 0 self.set_rot((Vector2(-1,-1)*direct[dir]).angle())
move(play_pos.normalized()*speed)

func _fixed_process(delta): step_move(“ui_up”,“N”,MAX_SPEED)
step_move(“ui_down”,“S”,MAX_SPEED)
step_move(“ui_left”,“W”,MAX_SPEED)
step_move(“ui_right”,“O”,MAX_SPEED)

Though, when I try to play it, it only shows the grid and sprite, unable to move…

Any helpful tips?

Your code is dificulty to ready. Use the forum format and don’t put more than one command in the same line.

var step = 0; var move = "stop" var playpos = Vector2(0,0)

You should use this instead:

var step = 0
var move = "stop" 
var playpos = Vector2(0,0)

As I can see, maybe you missed a ; after var move = “stop”. Using the good pratices of readability, as I showed could avoided that.

This solutes the problem?

gtkampos | 2018-01-25 19:44