help me it is about move the ball hit screen

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
phichet
Prole
Posts: 3
Joined: Tue Mar 02, 2010 7:01 am

help me it is about move the ball hit screen

Post 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
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

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

Post 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
phichet
Prole
Posts: 3
Joined: Tue Mar 02, 2010 7:01 am

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

Post 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
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 3 guests