Can someone guide me on a method how to add a sprite in JME, i have found this useful method to add individual images but i have no idea how to add a sprite from a spritesheet;
You need to create a quad with a material then add it to the gui node. You can then use AWT loader to change the image at run time.
private Geometry geometry;
private Material material;
//create quad
geometry = new Geometry(name, new Quad(1,1));
material = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
material.setTexture("ColorMap", new Texture2D(imageLoader.load(getImage(), true)));
geometry.setMaterial(material);
//set its size and locations in pixels
geometry.setLocalScale(new Vector3f(100,200, 1f));
geometry.setLocalTranslation(50,50,geometry.getLocalTranslation().z);
// add a new image to it
protected AWTLoader imageLoader;
// draw a fill rec
// create image
image = new BufferedImage(getTrueSize().width, getTrueSize().height,BufferedImage.TYPE_INT_ARGB);
// create graphics object from image
graphics = image.createGraphics();
// get font metrics
fontMetrics = graphics.getFontMetrics(getFont());
// set the font
graphics.setFont(getFont());
// fill image with color
graphics.setColor(Color.GRAY);
graphics.fillRect(0, 0, getTrueSize().width, getTrueSize().height);
// dispose of graphics object
graphics.dispose();
// set the image
protected void setImage(BufferedImage bufferedImage,boolean flipY){
material.setTexture("ColorMap", new Texture2D(imageLoader.load(bufferedImage, flipY)));
material.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
}
guiNode.attachChild(geometry);
This is just some base code. Have fun and play around with it.
Generally speaking, there are at least four possible ways to do it
to build you sprite sheet in runtime using texture atlas class
to recalculate texture coordinates for your quad mesh manually so they correspond to part of texture instead of full
to write custom shader and material to draw only part of texture without changing texture coordinates
to use some existing sprite library
But don’t you find a bit strange to use 3d game engine for 2d games? In my opinion tools designed for 2d could give you better results when making 2d game.
If your image is already a sprite sheet then you just need to set the texture coordinates on your quad. You can look inside Quad.java to see how it normally sets them to 0 and 1 and just copy that code to set them to a portion of your sprite sheet.
It’s easy, relatively fast, and will get you to your next problem whatever that is.
…no need to edit images on the fly. Package your sprite sheet up ahead of time.
You can also look at my library (Galago) for jME.
I have sprite sheet support in it. I also have 2d physics support and a build in GUI library.
I use this for all my android games in jME.