Having a bit of trouble here.

Blitz3D Forums/Blitz3D Programming/Having a bit of trouble here.

Jammer429(Posted 2014) [#1]
I've searched and found a couple of topics on this but I can't get any to work for me. I am pretty new to blitz and I was hoping someone could help me here. I'm trying to use an image as a button and no matter what I try I can't get it to work.





Midimaster(Posted 2014) [#2]
Your have to scan an area and not only a single point:

....
s_btnw =ImageWidth(start_btn)
s_btnh =ImageHeight(start_btn)
If ( MouseX() > s_btnx ) AND  ( MouseX() < (s_btnx+s_btnw) )
     If ( MouseY() > s_btny ) AND ( MouseY() < (s_btny+s_btnh) )
          over = True
     Endif
Endif
....


all mouse positions between left border of the image and right border of the image are possible. The same with top border and bottom boder...


Jammer429(Posted 2014) [#3]
thanks for the Reply. I tried this and still doesn't seem to be working for me.


Midimaster(Posted 2014) [#4]
There can be several reasons...

You should add some DEBUG lines for getting more information what happens (and what not...). This is a very important part of programming. "A lot of DEBUG lines" will save your time:

I show you what I mean:

Function MainMenu()                                               ; Main Menu
	PRINT "FUNCTION MAINMENU"
	s_btnx = 420
	s_btny = 600
	start_btn = LoadImage("Media\Gui\btn_start.png")
	DrawImage start_btn,s_btnx, s_btny
	DrawImage MainMenubg,0,0
	s_btnw =ImageWidth(start_btn)
	s_btnh =ImageHeight(start_btn)
	If ( MouseX() > s_btnx ) AND  ( MouseX() < (s_btnx+s_btnw) )
	     If ( MouseY() > s_btny ) AND ( MouseY() < (s_btny+s_btnh) )
	          PRINT "MOUSE INSIDE"
	          over = True
	     Endif
	Endif
	PRINT "OVER=" + over
	If MouseDown(1) And over = True
	          PRINT "MOUSE CLICK"
		End
	EndIf
	PRINT "END FUNCTION"
	
	Flip 
End Function


Keep this PRINT lines in your code until the development has finished. You can comment them out, if temporary not needed, but keep them in the code...

When you now start your code, you can watch, what happens.



There are a lot of additional mistakes in your code:

- you load the image again and again each frame. The images should be loaded once before the function is called:

Global start_btn = LoadImage("Media\Gui\btn_start.png")
Function MainMenu()
	PRINT "FUNCTION MAINMENU"
	s_btnx = 420
	s_btny = 600
	
	DrawImage start_btn,s_btnx, s_btny


- You should define SUPERSTRICT at the top of your code to take care about the quality of the variable definitions you use. With this line every variable has to be defined GLOBAL or LOCAL


-You should first check, whether MouseDown() is true. Then check ,where the mouse is:

If MouseDown(1) = True
	If ( MouseX() > s_btnx ) AND  ( MouseX() < (s_btnx+s_btnw) )
	     If ( MouseY() > s_btny ) AND ( MouseY() < (s_btny+s_btnh) )
	          PRINT "MOUSE CLICK"
		  End
	     Endif
	Endif
Endif



Jammer429(Posted 2014) [#5]
Thanks for your help. It wasn't working because I forgot to call the function in my main loop.