Login | Register

Links

October 14, 2006, 15:10 by Calle
[loading]
-->
This will explain how to make HTML-kind of links out of text which you draw on the screen, using the function draw_text or other. This might be useful in programs and games, for all kinds of stuff where you want to execute a specific event based on a variable, for which reason you can't make a static sprite.

To make this easier for you to use, I will make it as a script, which could be easily used. I will create it such that it will be called: link(x,y,"name";);

Very easy. First of all, in the script I will start to define these values...

xv = argument0;
yv = argument1;
nv = argument2;


And then continue with drawing the text:

draw_text(xv,yv,nv);

Now that all the visual stuff is handled (except if you want to add something stylish, like making the links blue; you could do that as well) we'll controll whether or not the mouse is in the area:

if (mouse_x<xv+string_width(nv)) && (mouse_x>xv) &&
(mouse_y<yv+string_height(nv)) && (mouse_y>yv) {
clickable = true;
}
else {
clickable = false;
}


So now we have a script for the draw event:
xv = argument0;
yv = argument1;
nv = argument2;
draw_text(xv,yv,nv);
if (mouse_x<xv+string_width(nv)) && (mouse_x>xv) &&
(mouse_y<yv+string_height(nv)) && (mouse_y>yv) {
clickable = true;
selected = nv;
}
else {
clickable = false;
}


In global left release event all you need to put is:
if (clickable == true) {
show_message("You've just clicked "+string(selected));
}

You can do anything inside the brackets, for an example, if nv is a url or file path, you could do like this:
if (clickable == true) {
execute_program(nv,"",true)
}

Ask any questions that you have on this article by commenting.

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games