M. kay
M. kay

Reputation: 39

Sprite not showing when drawn

I am attempting to build an android game using LibGDX. This is my first android app, so forgive me if what I am asking for is simple. I am trying to draw a sprite, but nothing seems to show up. I was thinking that maybe the sprite is drawn outside of the camera space, but even when I take the camera back(showing out of game area), I can't see the sprite. I have spent quite a while trying to find the problem, and I have not been able to. This is my Playstate Class:

public class PlayState extends State{
    private Man man;
    private Cannon cannon;
    private Texture cannontop;
    private Texture bg;
    private SpriteBatch sb;

    public PlayState(GameStateManager gsm) {
        super(gsm);
        cannon = new Cannon();
        cannontop = new Texture("Cannon.png");
        man = new Man(0, (Gdx.graphics.getHeight() / 4));
        bg = new Texture("background.png");
        cam.setToOrtho(false, 2160, 3840);

    }

    @Override
    protected void handleInput() {
       // finish later

    }

    @Override
    public void update(float dt) {
        man.update(dt);
        //cannon.update();
    }

    public void create(){
        sb = new SpriteBatch();
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.setProjectionMatrix(cam.combined);
        sb.begin();
        sb.draw(bg, cam.position.x - (cam.viewportWidth / 2), 0);
        sb.draw(man.getTexture(), man.getPosition().x, man.getPosition().y);
        //sb.draw(cannon.getTexture(), cannon.getPositionx(), cannon.getPositiony());
        cannon.getCannon().draw(sb);
        sb.draw(cannontop, (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2) - 23, (bg.getHeight() / 2) + (bg.getHeight() / 5));
        //sb.draw(cannon.getTexture(), (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannontop.getWidth() / 2) + 8);
        sb.end();

    }

    @Override
    public void dispose() {
        bg.dispose();
        man.dispose();
        cannon.dispose();
    }
}

The sprite that I am trying to draw is in the cannon class. This is my cannon class:

public class Cannon {
    private Texture cannend;
    private Texture bg;
    private Texture cannonmain;
    private SpriteBatch batch;
    private Sprite cannon;


    public Cannon(){
        cannonmain = new Texture("Cannon.png");
        bg = new Texture ("background.png");
        cannend = new Texture("cannonend.png");
        cannon = new Sprite(cannend, (bg.getWidth()/ 2) - (cannend.getWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannonmain.getWidth() / 2) + 8, (cannend.getWidth()), (cannend.getHeight()));
        cannon.setPosition((bg.getWidth()/ 2) - (cannend.getWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannonmain.getWidth() / 2) + 8);
        cannon.setRotation(70);
        cannon.setCenter((bg.getWidth()/ 2) , (bg.getHeight() / 2) + (bg.getHeight() / 5) + 3*(cannend.getHeight() / 4));
    }
    //public update(dt){
    //    stuffff
    // }


    public Sprite getCannon(){
        return cannon;
    }

    public float getPositionx(){
        return cannon.getX();
    }

    public float getPositiony(){
        return cannon.getY();
    }

    public void dispose(){
        cannonmain.dispose();
        cannend.dispose();
        bg.dispose();
    }

    public Texture getTexture(){
        return cannend;
    }

    public float getTextureWidth(){
        return cannend.getWidth();
    }
}

I apologise is the sprite/texture names are confusing. Please ignore the comments, as they are there for the future/alternative things that I have tried. I would like to know what I could do to get the Sprite to appear. Also, would the sprite rotate after I make it appear? Any help is massively appreciated. Thanks.

state class:

public abstract class State {
    protected OrthographicCamera cam;
    protected Vector3 mouse;
    protected GameStateManager gsm;

    protected State(GameStateManager gsm) {
        this.gsm = gsm;
        cam = new OrthographicCamera();
        mouse = new Vector3();

    }


    protected abstract void handleInput();
    public abstract void update(float dt);
    public abstract void render(SpriteBatch sb);
    public abstract void dispose();


}

Upvotes: 1

Views: 576

Answers (3)

Peanut Panda
Peanut Panda

Reputation: 76

You should draw your sprites with sprite.draw(spritebatch), instead of spritebatch.draw(sprite).

Also, what's the use of the State class? Is Playstate supposed to be a screen? Then you should probably implement the Screen interface in your class, which will then use the methods you want, like 'render', 'dispose', ...

Upvotes: 1

Vadim Ostanin
Vadim Ostanin

Reputation: 69

May be libgdx cannot draw sprites because duplicating any SpriteBatch.begin();
and duplicating SpriteBatch.end(); in stack of calls

Upvotes: 0

Deniz Yılmaz
Deniz Yılmaz

Reputation: 1094

Remove SpriteBatch sb parameter from render method Just use like

@Override 
public void render()

Upvotes: 0

Related Questions