Problems in Walking Astronaut (SOLVED)

Hi! I entered the code as described in the tutorial, but the astronaut did not move. Then I copied the text from the tutorial and pasted it into the script, but the astronaut still doesn’t move.

Can you check the names of the Animation Groups in your atlas? Maybe they are misspelled.

Can you also check your key bindings? This can be found in the file game.input_binding.

left_01-/assets/astronaut/left_01.png
key-down front
key-up back
key-left left

Can you zip your project and upload it here?

Did you add the script to the game object? Do you have any errors in the console? What if you add print (“hello”) in the init() function? Do you see “hello” in the console? Add another print(action_id) in on_input.

Add keys WASD - don’t move. 'Hello World" printed. I already made the ЭSide scroller tutorialЭ - everything was fine there.

I zipped folder " Walking astronaut tutorial", but your site writes : Sorry, the file you are trying to upload is not authorized (authorized extensions: jpg, jpeg, png, gif, mp3, lua, python, script, zip, cpp, mov, webm, m4v, mp4, ogg, pdf, txt).

Strange. Are you sure it’s .zip and not .rar?

I use 7zipWalking astronaut tutorial.zip (6.0 MB)

When I build your project, nothing happens and there is no output. I looked at your astronaut.script and I’ve found some mistakes.

The first one is that you wrote all your code inside an init function. You need to keep the premade functions (local on_input, local on_reload, etc…) separately. You can also have only one init function. https://www.defold.com/manuals/application-lifecycle/

Second one is that you’ve wrote msg.post("," , "acquire_input_focus"). Instead of a "," you either need to use "#" (if you want to talk to the current component) or "." (if you want to talk to the current game object). In this case we want to talk to the game object, so we use a ".".

The third thing is that you have two on_input functions. You can also have one of this function. One on_input function is empty while the other one has the walking script. When you remove the empty function, it will listen to the other on_input function.

Working script:

local speed = 150
function init(self)
	msg.post("." , "acquire_input_focus")
	self.dir = vmath.vector3()
	self.current_anim = nil
end

function on_input(self, action_id, action)
	if action_id == hash("front") then                        -- [1]
		self.dir.y = -1
	elseif action_id == hash("back") then
		self.dir.y = 1
	elseif action_id == hash("left") then                     -- [2]
		self.dir.x = -1
	elseif action_id == hash("right") then
		self.dir.x = 1
	end
end

function final(self)
	-- Add finalization code here
	-- Remove this function if not needed
end

function update(self, dt)
	if vmath.length_sqr(self.dir) > 1 then                   -- [1]
		self.dir = vmath.normalize(self.dir)
	end
	local p = go.get_position()                              -- [2]
	go.set_position(p + self.dir * speed * dt)   
	--animate the astronaut
	local anim = hash("idle")
	if self.dir.x > 0 then                                     -- [2]
		anim = hash("right")
	elseif self.dir.x < 0 then
		anim = hash("left")
	elseif self.dir.y > 0 then
		anim = hash("back")
	elseif self.dir.y < 0 then
		anim = hash("front")
	end

	if anim ~= self.current_anim then                          -- [3]
		msg.post("#sprite", "play_animation", { id = anim })   -- [4]
		self.current_anim = anim                               -- [5]
	end

	-- done animating            -- [3]
	self.dir = vmath.vector3()                               -- [4]
end


function on_message(self, message_id, message, sender)
	-- Add message-handling code here
	-- Remove this function if not needed
end

function on_reload(self)
	-- Add reload-handling code here
	-- Remove this function if not needed
end
4 Likes

Thank you. I understand - I need to learn Lua. ))

2 Likes

No problem. The tutorials are a good way to start. The Lua manual is also worth looking at :slight_smile: https://www.defold.com/manuals/lua/

2 Likes

I can delete projects only in a folder? In Defold I did not find such a function.

1 Like

Yes, if you want to delete a project, you need to go to the location where the project has been stored and delete the project folder.

1 Like