Page 1 of 1

simple left to right

Posted: Sat Oct 13, 2012 10:13 pm
by pancake
Hello!

Long time lurker first time poster, currently enjoying love, but having little bits of teething problems. Essentially what I would like to do seems simple enough but for some reason I can't get my head around it.

essentially I would like to be to have a block that starts from one point, scrolls to the next, and when it does reach that point it scrolls back to the original point and repeats, etc,.

This is an example of what I think it should be..

Code: Select all

function love.load()

thing = {}
thing.x = 30
thing.y = 30
thing.width = 40
thing.height = 40
thing.speed = 100

End

function love.update(dt)

If thing.x < 20 then
thing.x = thing.x + thing.speed * dt
end

If thing.x > 200 then
thing.x = thing.x - thing.speed * dt
end

function love.draw()

Love.graphics.setColor(255,255,255)
Love.graphics.rectangle("fill", thing.x,thing.y,thing.width,thing.height)

End
The answer to this problem is problem quite simple, but any help would be much appreciated!

Re: simple left to right

Posted: Sat Oct 13, 2012 10:59 pm
by Robin
Hello!

You need to keep track of what direction the thing is going, like this:

Code: Select all

function love.load()
    thing = {}
    thing.x = 30
    thing.y = 30
    thing.width = 40
    thing.height = 40
    thing.speed = 100
    thing.direction = 'right'
end

function love.update(dt)
    if thing.direction == 'right' then
        if thing.x > 200 then
            thing.direction = 'left'
        else
            thing.x = thing.x + thing.speed * dt
        end
    else
        if thing.x < 30 then
            thing.direction = 'right'
        else
            thing.x = thing.x - thing.speed * dt
        end
    end
end

function love.draw()
    love.graphics.rectangle("fill", thing.x,thing.y,thing.width,thing.height)
end
I also took the liberty to fix some capital letters and clean up your code a bit.