Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drawing an image for a score?  (Read 959 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2012-12-05 22:59:56 »

Lets say i have a sprite sheet of numbers 0-9 and i want to draw an image for the score of my game. So if my score is 250 i want to draw a 2 a 5 and then a 0. I am just extremely confused on how i would do this. i have been thinkning about it for a day now and I couldnt think of anything... any advice? or other ways of achieveing this goal?

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #1 - Posted 2012-12-06 00:17:47 »

It really isn't as difficult as you are making it seem. Each image has a width.
Let's say #2 has 40 pixels, and #5 has 30 pixels, and #0 has 35 pixels.

You choose a spot where you want to display the score. Let's say (10, 100).
Display #2 at (10, 100).

#5 has to be displayed right next to the first number drawn.
So you add the #2 width to the display value.
Display #5 at (10+40, 100) or (50, 100).

#0 has to be displayed past both numbers.
So you add the #2 and the #5 width to the first number drawn.
Display #5 at (10 + 40 + 35, 100) or (85, 100).

This will get your score images to display right next to each other. Hope it makes sense.

Online Agro
« Reply #2 - Posted 2012-12-06 00:27:13 »

Well, the above solution isn't the best, and it can be greatly simplified. For organization and efficiency, its best if you have a strip image where each number has the same width(align all the numbers to the right).

If you have an integer score, convert that to a string. Loop through each character in the string and use an approach like this:

1  
2  
3  
4  
5  
6  
7  
8  
int sx = 0, sy = 0;

for(int i = 0; i < str.length(); i++) {
    int pos = Integer.valueOf(str.substring(i, i));
    BufferedImage curnum = bitmapfont.getSubImage(pos * fontwidth, 0, fontwidth, fontheight);
    g.render(curnum, sx, sy, null);
    sx += fontwidth;
}


Play with that and you'll get what you want. Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #3 - Posted 2012-12-06 00:43:36 »

woot! your aweomse lol thats actually a very good solution !

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2012-12-06 02:53:48 »

Well, the above solution isn't the best, and it can be greatly simplified. For organization and efficiency, its best if you have a strip image where each number has the same width(align all the numbers to the right).

If you have an integer score, convert that to a string. Loop through each character in the string and use an approach like this:

1  
2  
3  
4  
5  
6  
7  
8  
int sx = 0, sy = 0;

for(int i = 0; i < str.length(); i++) {
    int pos = Integer.valueOf(str.substring(i, i));
    BufferedImage curnum = bitmapfont.getSubImage(pos * fontwidth, 0, fontwidth, fontheight);
    g.render(curnum, sx, sy, null);
    sx += fontwidth;
}


Play with that and you'll get what you want. Smiley

ok so after trying to implement it i am having some problems. when replacing your str string with my score string this error comes up: Cannot invoke substring(int, int) on the primitive type int .
also could you explain the curnum image to me? i dont understand it too well Sad

Online Agro
« Reply #5 - Posted 2012-12-06 03:07:25 »

Okay, so in your bitmap font, the numbers should go from 0 - 9. So basically, if you take your score, take each character and convert it to an integer, you can just get that frame from the bitmap font.

If the current number is 0, 0th frame of the bitmap font
If the current number is 8, 8th frame of the bitmap font

If you have an Integer score, String str = String.valueOf(score);

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #6 - Posted 2012-12-06 23:44:41 »

thanks a bunch man! this worked great

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!