Discovered this web site just today, about 4 hours ago or so to be honest, and I have to say Love is by far an awesome and fast game making tool hands down!
Anyways was fiddling around with it and using bits and pieces from a previous flash game i did which is sort of like a pong game i converted that gradually into Lua a language i haven't used before but its very similar to Php and a buncha other languages I did so i picked it up pretty fast.
if ballDir == "left" then
if x_ball <= 640 then
x_ball = x_ball - (speed_ball * dt)
end
if x_ball == 0 then
ballDir = "right"
end
else
if x_ball >= 0 then
x_ball = x_ball + (speed_ball * dt)
end
if x_ball >= 640 then
ballDir = "left"
end
end
This is code I used to make a ball basically go from side to side it works very nicely in flash ... sadly not in Love =(
What happens is this if you tell it to go right it will hit 640 and stop there. If you tell it to go left it will hit 0 and keep going .. which is rather funny to be honest haha
Anyways I think I need a fresh pair of eyes to check this bit out. Oh and if all of the source code is needed let me know I'll throw that on here as well
if ballDir == "left" then
x_ball = x_ball - (speed_ball * dt)
if x_ball <= 0 then
ballDir = "right"
end
else
x_ball = x_ball + (speed_ball * dt)
if x_ball >= 640 then
ballDir = "left"
end
end
orophite wrote:I've put it into the draw() function it worked nicely!
In draw()? I find it very odd you've got it to work in draw() and not in update(), where it fits better. I would suggest the same as rude and bartbes .
Thanks rude your code worked!
Better then doing it from the draw() function which gave me .. lets just say odd results from time to time haha
Yea I found it weird that that code worked in the draw() function as well I honestly thought that that function only runs ones to show objects and what not didn't think it was a loop of some kind.
Edit: Oh and no I didn't change the code I copied and pasted it directly from update(dt) to draw() as is and worked first run, here is the source code with the ball movement code commented out at the draw() function
function load()
bgImage = love.graphics.newImage("images/background.png")
x_bgImage = 320
y_bgImage = 240
pBar1 = love.graphics.newImage("images/pBar.png")
pBar2 = love.graphics.newImage("images/pBar.png")
x_pBar1 = 30
y_pBar1 = 240
x_pBar2 = 610
y_pBar2 = 240
speed_pBars = 250
ball = love.graphics.newImage("images/ball.png")
x_ball = 320
y_ball = 240
speed_ball = 100
ballDir = "right"
font = love.graphics.newFont(love.default_font, 12)
love.graphics.setFont(font)
end
function update(dt)
if ballDir == "left" then
x_ball = x_ball - (speed_ball * dt)
if x_ball <= 0 then
ballDir = "right"
end
else
x_ball = x_ball + (speed_ball * dt)
if x_ball >= (x_pBar2 - 5) then
ballDir = "left"
end
end
if love.keyboard.isDown(love.key_down) then
if y_pBar1 >= 480 then -- Checks if the bar is travelling outside the y bottom
y_pBar1 = 480 -- Sets y to bottom boundary limit
else
y_pBar1 = y_pBar1 + (speed_pBars * dt) -- Else it increase y
end
elseif love.keyboard.isDown(love.key_up) then
if y_pBar1 <= 0 then -- Checks if the bar is travelling outisde the y top
y_pBar1 = 0 -- Sets y to top boundary limit
else
y_pBar1 = y_pBar1 - (speed_pBars * dt) -- Else it decreases y
end
end
if love.keyboard.isDown(love.key_s) then
if y_pBar2 >= 480 then
y_pBar2 = 480
else
y_pBar2 = y_pBar2 + (speed_pBars * dt)
end
elseif love.keyboard.isDown(love.key_w) then
if y_pBar2 <= 0 then
y_pBar2 = 0
else
y_pBar2 = y_pBar2 - (speed_pBars * dt)
end
end
end
function draw()
-- Makes the ball bounce right to left
--[[if ballDir == "left" then
if x_ball <= 640 then
x_ball = x_ball - 4
end
if x_ball == 0 then
ballDir = "right"
end
else
if x_ball >= 0 then
x_ball = x_ball + 4
end
if x_ball >= 640 then
ballDir = "left"
end
end ]]
love.graphics.draw(bgImage, x_bgImage, y_bgImage) -- Creates the background
love.graphics.draw("The ball is at (" .. x_ball .. "," .. y_ball .. ")", 50, 50)
love.graphics.draw("The bar is at (" .. x_pBar2 .. "," .. y_pBar2 .. ")", 50, 67)
love.graphics.draw(pBar1, x_pBar1, y_pBar1) -- Creates the 1st player bar
love.graphics.draw(pBar2, x_pBar2, y_pBar2) -- Creates the 2nd player bar
love.graphics.draw(ball, x_ball, y_ball)
end