Page 1 of 1

Quick question about update(dt)

Posted: Sat May 30, 2009 9:32 am
by orophite
Hey everyone,

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.

Ah .. i better cut to the chase lol

Code: Select all

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

- Cheers

Re: Quick question about update(dt)

Posted: Sat May 30, 2009 9:48 am
by orophite
... oh I just hate these stupid situations haha

I managed to solve it just now.

I've put it into the draw() function it worked nicely!

Sorry for the bother

Re: Quick question about update(dt)

Posted: Sat May 30, 2009 10:57 am
by rude
How about this? (Untested)

Code: Select all

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

Re: Quick question about update(dt)

Posted: Sat May 30, 2009 10:59 am
by bartbes
Was going to suggest that, you just beat me to it..

Re: Quick question about update(dt)

Posted: Sat May 30, 2009 12:55 pm
by Robin
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 :ehem:.

Re: Quick question about update(dt)

Posted: Sat May 30, 2009 6:04 pm
by rude
I found that odd as well, but I assumed orophite had changed the code somehow.

Re: Quick question about update(dt)

Posted: Sun May 31, 2009 3:26 am
by orophite
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

Code: Select all

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

Re: Quick question about update(dt)

Posted: Sun May 31, 2009 4:09 am
by armando
hi there.

I discover love yesterday too. and start making a pong game.

pretty simple.

I wil try make this whith physcis things, like collitions, gravity. etc.


Nice engine o whathever love is


farewell.

armando, from mexico