Sprite Based Numbers

Posted by Shork on April 13, 2007, 11:51 a.m.

So the other day I got bored of using standard font for numbers in displays. I thought using sprites for the numbers would make it much more versatile, and I think real games do that most of the time anyway. So the other night I took five minutes to come up with this, someone else may have already done it, but I haven't seen it.

First, make a sprite with 11 images, zero through 9, and a blank square.

Now, decide on the largest number you want to draw and make variables accordingly. For example, I did 999, and in the create event I had Hundreds = 0, Tens = 0, and Ones = 0.

Now, get a number. I just did something where I clicked on the box and typed a number, but it could be any numerical variable. Do an if statement to make sure your variable is less than the largest number you planned for.

Do a series of floor functions to break that variable into its digits.

Hundreds = floor(Number/100)
Tens = floor((Number-(Hundreds*100))/10)
Ones = floor(Number-(Hundreds*100)-(Tens*10))

Once you have the number split up, you need to assign images to each digit. This is done using the sprite you drew before, if you did 0,1,2,3,4,5,6,7,8,9,blank, then it should be easy.

HundredsImage = Hundreds
TensImage = Tens
OnesImage = Ones

That will work just fine if you don't care about seeing things like 34 appearing as "034," but if you want to get rid of that extra zero, you'll need to add this:

if Hundreds = 0{
     HundredsImage = 10
     }
if Hundreds = 0 && Tens = 0{
     TensImage = 10
     }
if Ones = 0 && Tens = 0 && Hundreds = 0{
     OnesImage = 10
     }

That will make the zeros in the front appear blank. Don't use 10 if the blank square is not the tenth image in the sprite!

Now draw each sprite in the draw command, be sure to leave the y value the same and increase the x by the same amount every time, or you'll get the numbers all over the screen and it will look funny.

This can be used for numbers as large as you want, but the coding gets exponentially larger as you go, so be careful not to overdo it.

Hope this helps, tell me if it's wrong.

Comments

Tasm 17 years, 8 months ago

You could just use the font_add_sprite function.

E-Magination 17 years, 8 months ago

Shork just got owned.

NoodleNog 17 years, 8 months ago

Yep.

Killpill28 17 years, 8 months ago

HAHAHAHA, nice thoughXD

LunchTime 17 years, 8 months ago

GM letters are ugly :p

Shork 17 years, 8 months ago

Or you could use the fun method. Besides, this method of splitting numbers into their digits may have other uses.

Alpha Man 17 years, 8 months ago

I WOULD SAY THIS :

"What is a Shork…? Answer my question or BE TALKED TO EVEN MORE THAN I ALREADY INTEND ON SPAMMING YOUR BLOG LIKE THIS :

" Hi. "

I HAVED SPOKEDN ABOUT IT"

But instead… I SAY THIS :

" Hi. Actually I already knew how to do this. DOES THAT MAKE ME SMART? PLEASE ANSWER. "

Nods then might escape.

Shork 17 years, 8 months ago

What is a Shork? Shork is the Lord of Madness, the King of Chaos, the Destroyer of the Damned, and the Damner of the Destroyed! Shork will tear open your chest with his mind and consume the shrivled lump you call a soul!

Tasm 17 years, 8 months ago

Hope you know I didn't mean any disrespect :)