Moving blocks help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Beau192
Prole
Posts: 3
Joined: Wed Dec 20, 2017 12:56 am

Moving blocks help

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Moving blocks help

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Beau192
Prole
Posts: 3
Joined: Wed Dec 20, 2017 12:56 am

Re: Moving blocks help

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest