Page 1 of 1

Moving blocks help

Posted: Wed Dec 20, 2017 2:16 am
by Beau192

Code: Select all

function love.load()
ranposX = love.math.random(0, 500)
x = 250
y = 400

movey = 0
end

function love.update(dt)
if love.keyboard.isDown("right") then
x = x + 2
end

if love.keyboard.isDown("left") then
x = x - 2
end

if love.keyboard.isDown("left") and love.keyboard.isDown("s") then
x = x - 4
end

if love.keyboard.isDown("right") and love.keyboard.isDown("s") then
x = x + 4
end

if x < 0 then 
x = 0
elseif (x + 30) > 500 then
x = 500 - 30
end


repeat 
movey = movey + 3
until movey == 500
end

function love.draw()
love.graphics.rectangle("fill", ranposX, movey, 25, 25)
love.graphics.setBackgroundColor(39, 77, 126)
love.graphics.rectangle("fill", x, y, 30, 30)
end
I'm trying to figure out a way to make the block I am placing at the top of the screen to go to the bottom. I've tried the repeat until loop and it keeps glitching out. Help plz.

Re: Moving blocks help

Posted: Wed Dec 20, 2017 2:55 am
by zorg
Hi and welcome to the forums!

Simple issue, simple solution; Löve itself calls the two callbacks, love.update and love.draw, each frame, so you putting a loop in either one of them will run to completion under 1 frame (1/60th of a second, for a 60Hz screen, for example), which isn't what you want.

Figure out how long you want the movement to stretch out, then do something like the following:

Code: Select all

-- Commented out the unnecessary code from your examle.
--repeat 
movey = movey + 3 * dt -- move down by 3 pixels per second
--until movey == 500
That should work.

Do note that your input logic also might need some refinement; it works as it is at the moment, but along the way, you might realize it's not the best of ways how it currently is.

Re: Moving blocks help

Posted: Wed Dec 20, 2017 9:57 pm
by Beau192
Thanks for the help, but unfortunately it didn't work. I ended up fixing my own mistake. Me, as a beginner, thought it was more complex to do this. I did this to fix it:

Code: Select all

x = x + 5
Then, I added a sensor to move the block back to the top when it hit the bottom. I also finished the project, but I'm still unhappy about how it turned out. I'm thinking about adding more later.