You can also do it without sprites. Note that these two methods both have their good and bad sides. Choose which one you like most
. I prefer an method where I use an sensetive area (I just check if the mouse are greater then thisXCoordinate and smaller then anotherXCoordinate, and then I do the same with the y coordinates). It's quite easy. Lets say we want to have an button 'Play'. The first thing we want to do is to get the position of the mouse.
mousex() = returns the x position of the mouse
mousey() = returns the y position of the mouse
A good thing is to store the positions in variables. Lets just call the variable that stores the x position of the mouse for 'x', and the variable that stores the y position for 'y'. So now we got the code:
do
x = mousex()
y = mousey()
loop
Now this code can't do much. We may want some text to. Lets say we want it in the x coordinate 5 and y coordinate 17 (just picked two numbers, you can choose whatever coordinates you want). To do this, we can use the 'text' command. Code:
do
x = mousex()
y = mousey()
text 5, 17, "Play"
loop
To make an button out of this, you'll need two very handy commands:
text width( someText ) = will return the width of the given text (how many x coordinates wide it is).
text height( someText ) = will return the height of the given text (how many y coordinates it coveres).
We want the button to react if the mouse is over the text. This meens that the x coordinates of the mouse must be heigher or equal to the x coordinate where the text starts (5) and smaller or equal to the width of the text + the x starting position(5 + text width( someText )). To do that, we can use the code:
if x >= 5 and x <= 5 + text width( "Play" )
And to make sure it's in the button we must do the same thing on the y coordinates:
if y >= 17 and y <= 17 + text height( "Play" )
Now we pull this toughether with our other code, and then add so the text 'Your mouse is over the button' appears somewhere on the screen if the mouse is over the text.
do
cls
x = mousex()
y = mousey()
if x >= 5 and x <= 5 + text width( "Play" )
if y >= 17 and y <= 17 + text height( "Play" )
text 100, 100, "Your mouse is over the button"
endif
endif
text 5, 17, "Play"
loop
Note that I put the code where you detect if the mouse is over the text above where I draw the text. This is because then you will be able to change the color of the text if you want much easyer, but I won't tell you how here. Also note I've added an new command at the top, 'cls'. This will clear the screen.
Now we may want to detect of the button is pressed. There are one other command for that:
mouseclick() = returns which mouse button that is click. If no button is clicked, this command returns 0.
We add this command inside the code where we detect if the mouse is over the text.
do
cls
x = mousex()
y = mousey()
if x >= 5 and x <= 5 + text width( "Play" )
if y >= 17 and y <= 17 + text height( "Play" )
text 100, 100, "Your mouse is over the button"
if mouseclick() = 1 then text 1, 200, "The button is clicked"
endif
endif
text 5, 17, "Play"
loop
You can also make so it will only display 'Your mouse is over the button' if the button isn't pressed:
do
cls
x = mousex()
y = mousey()
if x >= 5 and x <= 5 + text width( "Play" )
if y >= 17 and y <= 17 + text height( "Play" )
if mouseclick() = 1
text 1, 200, "The button is clicked"
else
text 100, 100, "Your mouse is over the button"
endif
endif
endif
text 5, 17, "Play"
loop
This is how you can create an button with just some text commands. I hope you've found it usefull. Note that it might seem clumsy in this form, but if you, like I do, create an function out of it, it will be very dynamic. If you don't know what an function is, I suggest you look it up, becuase they are very good to work with.
And one thing more. Welcome to the forums.