Scaling with a mousepress.

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
Freonce
Prole
Posts: 8
Joined: Tue Jul 27, 2010 6:26 pm

Scaling with a mousepress.

Post 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.
Freonce
Prole
Posts: 8
Joined: Tue Jul 27, 2010 6:26 pm

Re: Scaling with a mousepress.

Post 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
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Scaling with a mousepress.

Post by thelinx »

The main issue is that you define the love.mousepressed callback inside the love.update callback.
Freonce
Prole
Posts: 8
Joined: Tue Jul 27, 2010 6:26 pm

Re: Scaling with a mousepress.

Post 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?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Scaling with a mousepress.

Post 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
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Scaling with a mousepress.

Post 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!
Help us help you: attach a .love.
Freonce
Prole
Posts: 8
Joined: Tue Jul 27, 2010 6:26 pm

Re: Scaling with a mousepress.

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

Who is online

Users browsing this forum: Google [Bot] and 4 guests