function love.load()
x = 200
y = 50
r = 10
speed = 100
end
function love.update(dt)
x = x - speed * dt
if x + r >= 800 then
x = x - speed * dt
elseif x - r <= 0 then
x = x + speed * dt
end
end
function love.draw()
love.graphics.circle("fill",x,y,r);
end
function love.load()
x = 200
y = 50
r = 10
speed = 100
left = true -- indicate the direction
end
function love.update(dt)
x = x - speed * dt
if (x + r >= 800) and not left then -- check for screen side and direction
left = not left -- reverse direction indicator
speed = 0 - speed -- reverse direction by reversing speed
elseif (x - r <= 0) and left then -- check for screen side and direction
left = not left -- reverse direction indicator
speed = 0 - speed -- reverse direction by reversing speed
end
end
function love.draw()
love.graphics.circle("fill",x,y,r);
end
you can also simply check if speed is greater than 0
function love.load()
x = 200
y = 50
r = 10
speed = 100
left = true -- indicate the direction
end
function love.update(dt)
x = x - speed * dt
if (x + r >= 800) and not left then -- check for screen side and direction
left = not left -- reverse direction indicator
speed = 0 - speed -- reverse direction by reversing speed
elseif (x - r <= 0) and left then -- check for screen side and direction
left = not left -- reverse direction indicator
speed = 0 - speed -- reverse direction by reversing speed
end
end
function love.draw()
love.graphics.circle("fill",x,y,r);
end
you can also simply check if speed is greater than 0
ball = {
x = 200,
y = 50,
r = 10,
speed = 100,
left = true
}
From then on you can refer to ball.x or ball.speed or ball.left or whatever so when the game gets bigger you can tell what belongs to who without having to resort to polluting the namespace with ballX or ballSpeed. You can also make it easy to add MORE balls by making it multi-dimentional, but I could explain that if you want me to. Not right now so as not to confuse you right out of the gate. Let's say it will make it easy to have as many balls all updating and drawing at once and reusing the same code.