Any good way to find the position of a SpriteFont character?

0 favourites
  • 4 posts
From the Asset Store
Vector illustration of a UFO with an alien in three different positions.
  • Basically, I'm trying to make a system which replaces tags in text with an image. For example, right here I can Include right inline with the text. I'd like to be able to do something similar to that at runtime in C2, but I've come across a problem.

    It's easy enough to identify the tags, read them, replace them with whitespace, etc. But, when I want to insert the actual sprite, I can't actually find the coordinates of the character it should be placed on top of! Basically, after doing the tag processing, I end up with a space character and the index of that character in the text. I simply can't find a good way to translate that text index into an actual coordinate. I suppose it'd be possible to loop through all the characters and add up their individual character widths, but it gets *really* dodgy when line breaks get into the mix. I'd like to potentially support this for regular text, too, and that's even more impossible since I can't even access character widths.

    One workaround would of course be just to substitute the icon in for an unused character in the spritefont file itself, but there are a few limitations there. It's a bit of a pain to keep track of (æ = etc) but the biggest issue is that I lose the ability to animate the icon.

    Any thoughts?

  • Interesting problem. It seems like it would be very difficult to handle reliably with events, what with scaling, justification, kerning, line breaks, etc. Though, the spritefont object itself has to deal with all this...maybe a new expression, CharX/Y (char, index)?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here's a less than optimized way to do it. Perhaps you can build upon it to satisfy your need.

    The same method could probably be used with normal text, using ExecJS with a small script that calls the measureText method, although it would prove twice the challenge.

  • Interesting! Magistross great job! That's basically what I had in mind to do but it's just so... awkward to have to go to all that effort.

    I think for my currently planned application I'll just go with the 'replace a Unicode character' approach.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)