Page 1 of 1

Scaling with a mousepress.

Posted: Tue Jul 27, 2010 6:57 pm
by Freonce
Hi guys,
New Love type person here and I'm trying to figure all this out as best I can, but I've run into a problem. I searched the forums and haven't found this specific problem so thanks in advance.

I'm trying to get an image to scale larger over time as a mouse button is pressed down. As it stands the image only scales at a fixed amount once on a press, but when I use key presses it keeps getting bigger like I want.

Code: Select all

function love.update(dt)
    x, y = 400 , 300
	if love.keyboard.isDown("up") then
      sy = sy + 10 * dt -- this works
	  sx = sx + 10 * dt -- this works too
   end
   
   if love.keyboard.isDown("down") then
      sy = sy - 10 * dt -- this works too
	  sx = sx - 10 * dt -- this works too
   end
   
   function love.mousepressed(x, y, button)
   if button == 'l' then
	  sx = sx * 100 * dt -- not working like I want
	  sy = sy * 100 * dt -- not working like I want
   end
end
I'm sure this is a pretty nub question, but its driving me nuts. Thanks again.

Re: Scaling with a mousepress.

Posted: Tue Jul 27, 2010 7:17 pm
by Freonce
lol....ok I guess I jumped the gun a little. After some more poking around I found a solution to my problem, and i figured I'd share.

I replaced the love.mousepressed with love.mouse.isDown and had the incremental value added not multiplied.

Code: Select all

function love.update(dt)
    x, y = 400 , 300
	if love.keyboard.isDown("up") then
      sy = sy + 10 * dt -- this would increment scale by 10 per second
	  sx = sx + 10 * dt -- this would increment scale by 10 per second
   end
   
   if love.keyboard.isDown("down") then
      sy = sy - 10 * dt -- this would increment scale by 10 per second
	  sx = sx - 10 * dt -- this would increment scale by 10 per second
   end
   
  if love.mouse.isDown("l") then
    text = "Mouse button right is pressed"
	  sx = sx + 10 * dt -- gets bigger over time now
	  sy = sy + 10 * dt -- gets bigger over time now
	end
	  
end
Off to a good start I'd say. :P

Re: Scaling with a mousepress.

Posted: Tue Jul 27, 2010 7:29 pm
by thelinx
The main issue is that you define the love.mousepressed callback inside the love.update callback.

Re: Scaling with a mousepress.

Posted: Wed Jul 28, 2010 7:32 pm
by Freonce
The main issue is that you define the love.mousepressed callback inside the love.update callback.
Care to explain why? What I've seen from other examples shows the the love.mousepressed inside the update function. Doesn't this make sure that the key press is checked every second?

Re: Scaling with a mousepress.

Posted: Wed Jul 28, 2010 8:03 pm
by kikito
The function you need donesn't change.

You are re-defining the function over and over on update, that repeats itself many times per second.

In a way, what you are doing is equivalent to this:

Code: Select all

while true do
  x = 1
  print('The value of x is ' .. x)
end
Since the value of x doesn't change, you can put it outside the loop:

Code: Select all

x = 1 -- this is executed only once, so your code is more efficient
while true do
  print('The value of x is ' .. x)
end

Re: Scaling with a mousepress.

Posted: Thu Jul 29, 2010 7:40 am
by Robin
Freonce wrote:Care to explain why? What I've seen from other examples shows the the love.mousepressed inside the update function. Doesn't this make sure that the key press is checked every second?
Which examples? We need to know which ones propagate these horrible lies so we can extinguish them.
EXTINGUISH!

Re: Scaling with a mousepress.

Posted: Thu Jul 29, 2010 9:33 pm
by Freonce
Which examples? We need to know which ones propagate these horrible lies so we can extinguish them.
I was working off of the Hamsterball tutorial. I think it's right and its working now so no help is needed with this problem anymore thanks. I've actually moved on to the physics stuff and am trying to piece together a little test game.