Question regarding 2D sprite

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;

        Node node = new Node(name);
        Picture pic = new Picture(name);
        Texture2D tex = (Texture2D) assetManager.loadTexture("Textures/"+name+".png");
        pic.setTexture(assetManager, tex, true);
        float width = tex.getImage().getWidth();
        float height = tex.getImage().getHeight();
        pic.move( -width/2, -height/2, 0);
         Material picMat = new Material(assetManager, "Common/MatDefs/Gui/Gui.j3md");
        node.setMaterial(new Material());

        node.setUserData("radius", width / 2);

        return node;

Sorry if i sound noob because i really am haha :sweat:

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)));

      //set its size and locations in pixels
      geometry.setLocalScale(new Vector3f(100,200, 1f));

     // 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
     // fill image with color
     graphics.fillRect(0, 0, getTrueSize().width, getTrueSize().height);
     // dispose of graphics object

    // set the image
    protected void setImage(BufferedImage bufferedImage,boolean flipY){
      material.setTexture("ColorMap", new Texture2D(imageLoader.load(bufferedImage, flipY)));


This is just some base code. Have fun and play around with it.

if i use the AWTLoader will i be able to make the game work on android?

I don’t develop android games so I can’t answer that one for you. You could always just try it and see what it does.

but in case it does not work is there any other way ?:slight_smile:

No idea , I don’t mess around with around android games. Try checking the wiki.

I cant find anything useful on the wiki… any suggestion welcome :persevere:

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 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.

1 Like

No not at all since i want to make a 2D game with some elements in 3d.

I have a spritesheet library…

EDIT: forgot the sample application:

And a unfinished “book” to get started:

There are some interesting features, like animations, that are yet to be documented though

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.

Here is my playstore profile: