RVAGameJams/learn2love

[Chapter 5] Left or right movement no longer works

Closed this issue · 4 comments

Hi, this is my first git issue (yay).

Great tutorial, much thanks. My problem is that as soon as I entered chapter 5 code, left or right movement no longer works, even though pausing with space works just fine. Any ideas?

@matejsarlija thanks for the comments. Are you referring to the paddle movement on Breakout (part 5)? The example code I cannot find an issue. Can you share your code, either in a git repo or zip file or something so that I can help debug?

@jaythomas https://github.com/matejsarlija/breakoutLOVE here is my copy repo, I've even tried bluntly copying the code from this one, didn't work. I'm brainfogged, thanks so much.

Looks like your code is halfway between part 4 and part 5. In part 4, input.lua kept track of what keys were pressed, but in part 5 the state.lua was introduced as a place to centralize all your game's state in one place.

Next up, update paddle.lua to require state instead of input. The entity.update function now needs to reference state.button_left and state.button_right to tell if the player has pressed any buttons.

Take a look at your paddle entity. It is trying to load the input service and read the button states:

	entity.update = function(self, dt)
		-- Don't move if both keys are pressed. Just return
		-- instead of going through the rest of the function
		if input.left and input.right then
			return
		end
		local self_x = self.body:getX()
		if input.left and self_x > left_boundary then
			self.body:setLinearVelocity(-entity_speed, 0)
		elseif input.right and self_x < right_boundary then
			self.body:setLinearVelocity(entity_speed, 0)
		else
			self.body:setLinearVelocity(0, 0)
		end
	end

Try these changes:

diff --git a/entities/paddle.lua b/entities/paddle.lua
index ba466db..02d3ee9 100644
--- a/entities/paddle.lua
+++ b/entities/paddle.lua
@@ -1,4 +1,4 @@
-local input = require('input')
+local state = require('state')
 local world = require('world')
 
 return function(pos_x, pos_y)
@@ -27,13 +27,13 @@ return function(pos_x, pos_y)
        entity.update = function(self, dt)
                -- Don't move if both keys are pressed. Just return
                -- instead of going through the rest of the function
-               if input.left and input.right then
+               if state.button_left and state.button_right then
                        return
                end
                local self_x = self.body:getX()
-               if input.left and self_x > left_boundary then
+               if state.button_left and self_x > left_boundary then
                        self.body:setLinearVelocity(-entity_speed, 0)
-               elseif input.right and self_x < right_boundary then
+               elseif state.button_right and self_x < right_boundary then
                        self.body:setLinearVelocity(entity_speed, 0)
                else
                        self.body:setLinearVelocity(0, 0)

Unfortunately when variables go undefined in lua you sometimes don't get an error. What I do when I have a problem with a particular entity I start there and try to print out all the variables I can. Hope this helps though!

Thank you so much. I'll figure it out.