Page 1 of 1

[SOLVED] Problem with pictured menu

Posted: Sun Feb 02, 2014 7:57 pm
by ellessaar
How can I make menu pictures change? I mean:

I've got a menu (table) and each button in this menu is an image. I want to change image into e.g. a bigger one if the mouse position is on the picture. The question is ...
... how? I made some button_check() function checking the position of mouseX & Y but it doesn't work... Don't know what's wrong.

The code is quite large so I attach it as a .rar to my message (it's all in here, with the pictures).
It's my first post here so, please, forget all my mistakes.

Re: Problem with pictured menu

Posted: Sun Feb 02, 2014 9:03 pm
by lachlaan
Try passing the mouse X and Y coordinates to the check function instead of just using the same name in the separate modules.

Line 43 in menu.lua changes to

Code: Select all

function button_check(mouseX, mouseY)
And line 46 of main.lua changes to

Code: Select all

      button_check(mouseX, mouseY)

Re: Problem with pictured menu

Posted: Sun Feb 02, 2014 9:11 pm
by ellessaar
lachlaan wrote:Try passing the mouse X and Y coordinates to the check function instead of just using the same name in the separate modules.

Line 43 in menu.lua changes to

Code: Select all

function button_check(mouseX, mouseY)
And line 46 of main.lua changes to

Code: Select all

      button_check(mouseX, mouseY)
Nothing's happening ;/

Re: Problem with pictured menu

Posted: Sun Feb 02, 2014 9:20 pm
by Helvecta

Code: Select all

for i,v in ipairs(button) do
      if mouseX > v.x and
         mouseX < v.x + v.pic:getWidth() and
         mouseY > v.y and
         mouseY < v.y + v.pic:getHeight() then
           if v.id == "start" then
              start = startSet
           elseif v.id == "exit" then
              exit = exitSet
           end
      else start = startPic
           exit = exitPic
      end     
  end
This loop nearly works correctly; it understands when you're hovering over which image, but what you set is not something that is drawn later on, so it's as if nothing happened. setting exit to exitPic or start to startSet doesn't do anything, because the drawing is relying on the button table. You need to apply changes to v.pic instead, like in the below code:

Code: Select all

function button_check()
  for i,v in pairs(button) do
      if mouseX > v.x and
         mouseX < v.x + v.pic:getWidth() and
         mouseY > v.y and
         mouseY < v.y + v.pic:getHeight() then
           if v.id == "start" then
			  v.pic = startSet
		   elseif v.id == "exit" then
              v.pic = exitSet
           end
      else 
		if v.id == "start" then
			v.pic = startPic
		elseif v.id == "exit" then
			v.pic = exitPic
		end
       end     
  end
end
This makes the start button work, but not the exit button. To fix the exit button, go look at your spawn code:

Code: Select all

  button_spawn(320-exit:getWidth()/2,240+hsPic:getHeight(),exit,"quit")
the ID on this button is declared as "quit", but the button_check() function wants the ID to be "exit"! Changing the ID from "quit" to "exit" fixes the problem for the exit button.

I also went ahead and fixed the high score button, even though you haven't put any code in for it yet, check out the attachment for your patched code!

Good luck -- and welcome to the forums! :)

Re: Problem with pictured menu

Posted: Sun Feb 02, 2014 9:33 pm
by ellessaar
Thank you so much, it works fine! :)