Atlas Bug. When i Draw my Image, it draw all the images

Hi Guys. Im having a little trouble with my Atlas class.
I tried solving by myself but, still NO-OK.

What happens, is that, when i ask to draw anything on screen, it draws all the images in atlas together, side-by-side.

So i tried to remove the static method and pass it as a parameter…

So i did this :

SpriteManager class creates the Atlas Class.

Plane Class Receives SpriteManager as a paramater.

Well, i will post those 3 classes involved. I would love if someone can give me a hand… I starting to think in making without Atlas idk…

So, if anyone could have a look :

package Atlas;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.PixmapPacker;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.math.Rectangle;
import java.util.HashMap;

public class Atlas {
        private TextureAtlas atlas ;
        private final PixmapPacker packer = new PixmapPacker(512, 512, Format.RGBA8888, 0, false);
        private final HashMap<String, AtlasRegion> regions = new HashMap<>();
        private final HashMap<String, Rectangle> rectangles = new HashMap<>();
         
        public void generateAtlas() 
        {
                
                packer.pack("plane", new Pixmap(Gdx.files.internal("planeTR.png")));
                packer.pack("missileArthur", new Pixmap(Gdx.files.internal("missileArthurTR.png")));
                packer.pack("enemyOranglePlane", new Pixmap(Gdx.files.internal("enemyOrangePlane.jpg")));
 
                atlas = packer.generateTextureAtlas(TextureFilter.Nearest, TextureFilter.Nearest, true);
                
                for (AtlasRegion r : atlas.getRegions()) {
                    regions.put(r.name, r);
                    rectangles.put(r.name,getRepRectangle(r.getRegionWidth(),r.getRegionWidth()));
                }
                
        }
 
        public void dispose() {
                atlas.dispose();
                packer.dispose();
        }
 
        public AtlasRegion get(String name) {
                return regions.get(name);
        }
        
        //Recupera os retangulos para usar na colisao
        public Rectangle getRectangle(String name,int posX,int posY)
        {
            Rectangle rectGet = rectangles.get(name);
            rectGet.setX(posX);
            rectGet.setY(posY);
            
            return rectGet;
        }
        
        //Guarda os retangulos na Lista
        private Rectangle getRepRectangle(int width,int height)
        {
           Rectangle rect = new Rectangle(0,0,width,height);
           return rect;
        }
}
package Atlas;

import View.ShooterGame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;

/**
 *
 * @author André Vinícius Lopes
 */
public class SpriteManager {

    private Atlas atlas;
    private Texture explosionTexture;
    private ExplosionModel explosionModel;

    private ShooterGame shooterGame;
    
    public SpriteManager(ShooterGame sg) {
        atlas = new Atlas();
        //
        atlas.generateAtlas();
        explosionTexture = new Texture(Gdx.files.internal("Explosion2spritesheet.png"));
        //
        explosionModel = new ExplosionModel(this);
        //
        shooterGame = sg;
    }

    public Atlas getAtlas() {
        return atlas;
    }

    public Texture getExplosion() {
        return explosionTexture;
    }

    public ExplosionModel getExplosionModel() {
        return explosionModel;
    }
}

package Models;

import Atlas.SpriteManager;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Rectangle;


/**
 *
 * @author André Vinícius Lopes
 */
public class Plane {

    private int xPos, yPos;

    private Texture planeTexture;
    
    private int TEXTURE_WIDTH,TEXTURE_HEIGHT;
    
    private Rectangle planeCollisionRectangle;
    
    public int MOVE_RATE = 2;
    
    public Plane(SpriteManager sm) {
      
        planeTexture = sm.getAtlas().get("plane").getTexture();
        TEXTURE_HEIGHT = planeTexture.getHeight();
        TEXTURE_WIDTH = planeTexture.getWidth();
        
        xPos = 5;
        yPos = 8;
        planeCollisionRectangle = new Rectangle(xPos,yPos,TEXTURE_WIDTH,TEXTURE_HEIGHT);
    }

    
    public void render(SpriteBatch sb)
    {
      sb.draw(planeTexture, xPos, yPos);
    }
    
    public void moveRight()
    {
      xPos = xPos + MOVE_RATE;
      
      if(xPos > Gdx.graphics.getWidth())
      {
          xPos = - TEXTURE_WIDTH + MOVE_RATE; 
      }
      
    }
    
    public void moveLeft()
    {
        xPos = xPos - MOVE_RATE;
        
        if(xPos < 0)
        {
         xPos = Gdx.graphics.getWidth() - MOVE_RATE;
        }
        
    }
    
    
    
    
    public int getTEXTURE_WIDTH() {
        return TEXTURE_WIDTH;
    }

    public int getTEXTURE_HEIGHT() {
        return TEXTURE_HEIGHT;
    }

    public Rectangle getPlaneCollisionRectangle() {
        planeCollisionRectangle.x = (xPos);
        planeCollisionRectangle.y = (yPos);
        return planeCollisionRectangle;
    }

    public Texture getPlaneTexture() {
        return planeTexture;
    }

    public int getxPos() {
        return xPos;
    }

    public void setxPos(int xPos) {
        this.xPos = xPos;
    }

    public int getyPos() {
        return yPos;
    }

    public void setyPos(int yPos) {
        this.yPos = yPos;
    }
}

Heres the Bug :

http://img38.imageshack.us/img38/3668/atlasbug.jpg

Ah, i forgot this Class :

package View;


import Atlas.Explosion;
import Atlas.SpriteManager;
import Logic.GameUpdate;
import Models.Plane;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.awt.Font;
import java.util.ArrayList;


/**
 *
 * @author André Vinícius Lopes
 */
public class ShooterGame implements ApplicationListener {

    private SpriteBatch spritebatch;
    private BitmapFont font;
    private SpriteManager spriteManager;
    private GameUpdate gameUpdate;
    
    private Plane plane;
    
    
    @Override
    public void create() 
    {
       spriteManager = new SpriteManager(this);
       
       spritebatch = new SpriteBatch();
       
       gameUpdate = new GameUpdate(this);
       gameUpdate.addNewExplosion(0,0);
       font = new BitmapFont();
       
       //
       plane = new Plane(spriteManager);
       
    }

    @Override
    public void render() {
        gameUpdate.update();
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     
        //START DRAW//
        spritebatch.begin();
        drawExplosions();
        plane.render(spritebatch);
        
        //End Draw :: 
        spritebatch.flush();
        spritebatch.end();
        //END DRAW//
    }


    private void drawExplosions()
    {
        ArrayList<Explosion> explosions = gameUpdate.getExplosions();
        for(int i = 0; i < explosions.size();i++)
        {
         explosions.get(i).render(spritebatch);
        }
    }
  



    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
    
    /////////////////////////////////////////////////
    public SpriteBatch getSpritebatch() {
        return spritebatch;
    }

    public SpriteManager getSpriteManager() {
        return spriteManager;
    }
    
    
}

Looks like it’s because all your plane images have the word “plane” in their names, so

planeTexture = sm.getAtlas().get("plane").getTexture();

is getting all the plane images and stuffing it into one texture…not absolutely sure but that’s what it looks like. Instead, better to load the default sprite

sm.getAtlas().get("plane/default").getTexture()

and then just get different entries from the Atlas when needed.

Texture planeLeft = sm.getAtlas().get("plane/left").getTexture()

yay ty for the reply.

But what about the missileArthur.
It doesnt have plane on it :stuck_out_tongue:

I will try what u said.

Same error.
I noticed that, if i make this value = to the plane img size, it stops happening…

  private  PixmapPacker packer = new PixmapPacker(64,64, Format.RGBA8888, 0, false);

So i better not use Atlas. I dont trust it :stuck_out_tongue:

Or maybe im doing something wrong, but idk what.

The problem is that you are drawing a Texture object (planeTexture), which renders the whole texture atlas. Instead, you need to draw a region, i.e. the region you got from the TextureAtlas.

Learn about the differences between pixmaps, textures, and texture regions here:

Oh! ok i printed that tutorial, gonna read it!

Thanks man :smiley: