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 :