simple left to right

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
pancake
Prole
Posts: 1
Joined: Sat Oct 13, 2012 3:35 pm

simple left to right

Post 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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: simple left to right

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 4 guests