What is the most elemental way to move an Sprite in four directions?

I’m a total beginner and I I don’t understand how is possible to move an game object with the movement arrows.
I have some code examples but when I use those samples in the object script file, it doesn’t works.
Please, I need a code sample with the most elemental way to move my sprite. I know Lua well, but not the Defold API’s.
Thanks!!!

Moving by vectors may not be super easy if you don’t know how vectors work but once you do understand they are absolutely the easiest and most convenience way.

First you need to add the keys you want to detect into your game.input_binding file.

Example https://github.com/subsoap/defrs/blob/master/input/game.input_binding

Then you need to add a game object to your active collection, add a script as a component to that GO, and in that script have it get input and add detection for the keys.

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	if action_id == hash("w") then
	end
	if action_id == hash("a") then
	end
	if action_id == hash("s") then
	end
	if action_id == hash("d") then
	end	
end

Then going from there you setup your vectors and various variables and then apply them while you are holding down input. The input flag is cleared at the end of each frame but then enabled again on the input step of the app lifecycle if input is detected.

Here’s a 4 way direction movement which has the object slowly rotating toward the new target direction.

function init(self)
	msg.post(".", "acquire_input_focus")
	self.direction = vmath.vector3(1,0,0)
	self.position = vmath.vector3(100,100,0)
	self.turn_speed = 0.085
	self.moving = false
	self.speed = 10
	go.set_position(self.position)
end


function update(self, dt)
	self.direction = vmath.normalize(self.direction)
	pprint(self.direction)
	if self.moving == true then
		self.position = self.position + self.direction * self.speed
		go.set_position(self.position)
	end
	self.moving = false
end


function on_input(self, action_id, action)
	if action_id == hash("w") then
		self.direction.y =  self.direction.y + self.turn_speed
		self.moving = true
	end
	if action_id == hash("a") then
		self.direction.x = self.direction.x - self.turn_speed
		self.moving = true
	end
	if action_id == hash("s") then
		self.direction.y = self.direction.y - self.turn_speed
		self.moving = true
	end
	if action_id == hash("d") then
		self.direction.x = self.direction.x + self.turn_speed
		self.moving = true
	end	
end

If you want to add arrow key support too it’s as easy as changing the on_input ifs to for example

if action_id == hash("d") or action_id == hash("down") then

The hash names are again based on what you put in the game.input_binding file.

If you want the above example to have the new direction set instantly you could naturally increase the turn speed so that it’s high enough where the new direction change is instant.

This is one way to do it there are more.

5 Likes

There’s an 8-way movement demo in the new Examples section.

I have a demo here and some code here to do the same thing.

5 Likes