How to use length of a string for collision detection

So I have this code that displays a string, and whenever the mouse hovers over it, then it changes colour.

if (mouseX > 40 && mouseX < text.length() * 17)

The text size is 25. However, it seems that text.length() * 17 only works for some strings. On shorter strings, you have to move your mouse closer, and on longer strings, you have to move your mouse further.

So is there a way that I can use a string’s length to set boundaries to the string? Thanks

1 Like

When I have a question like this, I usually start by reading through the reference:

Maybe look for a function that would return the width of your text?


Thanks! I will do that in the future

1 Like