Page 1 of 1

help me it is about move the ball hit screen

Posted: Fri Apr 13, 2012 4:55 am
by phichet
here is code when a ball hit screen it is can not change direction

Code: Select all

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

Re: help me it is about move the ball hit screen

Posted: Fri Apr 13, 2012 5:23 am
by Wojak

Code: Select all

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

Re: help me it is about move the ball hit screen

Posted: Fri Apr 13, 2012 7:37 am
by phichet
Wojak wrote:

Code: Select all

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
thank you very much

Re: help me it is about move the ball hit screen

Posted: Sat Apr 14, 2012 6:43 am
by Jasoco
Let me give you a neat tip for the future: Since all those variables belong to your "ball", you should put them all in an easy to use table like so...

Code: Select all

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.