Page 1 of 1

What is the best way to create a bunch of images that do something when clicked?

Posted: Sun Jun 14, 2020 6:02 pm
by Taycamgame
Basically, I'm making a title screen menu. I'm using images for the menu options (e.g. start game, options).

What's the best / most efficient way to run a piece of code after clicking on these images? One idea is that I just write the same code multiple times to have different image coordinates and run different code for each one. However, I have a feeling it would that using classes in some way would make it much more efficient, but I don't know how to use them (both in general and in this context).

Thanks :)

Re: What is the best way to create a bunch of images that do something when clicked?

Posted: Sun Jun 14, 2020 8:10 pm
by pgimeno
What you want is a (very barebones) GUI. Basically, clickable image buttons.

Your code needs to be able to find the position and size of each image, then when the mouse button goes down, go through all of them and check whether the mouse is inside one.

Traditionally, GUIs have tables with the position and size of each element in the GUI, and query all of them every time the mouse is pressed. You could do that too, have a table for each image and then a container table with all the other tables.