Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  Maps for storing sprite data?  (Read 1234 times)
0 Members and 1 Guest are viewing this topic.
Offline gbeebe

Senior Devvie

Medals: 5
Projects: 1

« Posted 2011-12-02 04:55:32 »

Ok, so this might be a bit confusing for me to describe.
I've got a character/player class named Chara.  Inside it is a public Map that holds a Sprite class, which holds the Image, offsets and such.  Which is nice, because I can refer to a sprite frame by name.  To draw a sprite, I'm doing something like this:
Player1 is a Chara class, btw...
g.drawImage(Player1.getSprite("faceLeft"), Player1.getX("faceLeft") - Player1.getOffX("faceLeft")+ mapOffX, Player1.getY("faceLeft") - Player1.getOffY("faceLeft") + mapOffY, null);

The Chara class gets the Sprite class from the Map by using the string passed to the procedures.  I do that to draw the body, and then something similar to draw the arm('cause it rotates separately) and the graphic of the weapon the sprite is holding,  Which is fine right now, but I'm worried that after I get to the point where I'm adding other players and NPC into the mix, this might be really slow.  Are Maps something I should try to avoid?  I've seen some articles on speed tests with Maps, they don't scare me, but they're not game related either.  Is there something else I should be doing instead?  
Offline roland
« Reply #1 - Posted 2011-12-02 06:45:41 »

Hashmaps are very fast - order (1), much better than arraylists(order n) etc. Because you don't search through the all the elements, it takes you instantly to the right spot in the map.

It probably will be fine, but if you are really concerned you can speed it up by not using a map and just having a member variable for each sprite

in Player class:
Sprite faceLeft;
Sprite faceRight;

I would have the sprite class storing offset and stuff, instead of the map holding it.

Sprite faceLeft = Player1.GetFaceLeft();
so g.drawImage(faceLeft.GetImage(), faceLeft.GetX(), etc

If you don't know the names of the sprites beforehand, you might use a map, but you should still do what I said about storing the offsets and everything in the class, so then you only need 1 map get call instead of 5 for each player.


Sprite faceLeft = player1.GetSpriteFromMap("faceLeft")
g.DrawImage(faceLeft.GetImage(), ...);

Offline gbeebe

Senior Devvie

Medals: 5
Projects: 1

« Reply #2 - Posted 2011-12-03 02:25:37 »

Nice, I never thought to have a function to return the sprite class, it's kinda' like a pointer then  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2011-12-03 18:21:13 »

How about your rendering at Player1 class. Then you can just minimize it to Player.render(g,mapOffX,mapOffY);

Also you could make myOwnGraphicObject class.
That would encapsulate sprite, position, offsets and other data if needed.
And some utility methos that would calcualte position form x and offSetX

So if faceLeft would be myOwnGraphicObject the code look like this.
void Player.render(Graphics g, float mapOffX, float mapOffY){
g.drawImage(faceLeft.sprite, faceLeft.getPosX() + mapOffX, faceLeft.getPosY() + mapOffY, null);

Your way seems to be too vocal with hardly any benefits.

Pages: [1]
  ignore  |  Print  

EgonOlsen (59 views)
2018-06-10 19:43:48

EgonOlsen (41 views)
2018-06-10 19:43:44

EgonOlsen (61 views)
2018-06-10 19:43:20

DesertCoockie (240 views)
2018-05-13 18:23:11

nelsongames (142 views)
2018-04-24 18:15:36

nelsongames (141 views)
2018-04-24 18:14:32

ivj94 (883 views)
2018-03-24 14:47:39

ivj94 (144 views)
2018-03-24 14:46:31

ivj94 (795 views)
2018-03-24 14:43:53

Solater (159 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!