jordan white
jordan white

Reputation: 37

libgdx, java - trying to call a .render() from another class

Hello I am trying to draw "whiteballoon" in main from a serperate class called whiteBalloon. The point is , whenever I call this class's method render() I want to be able to have multiple sprites on screen, clones of each other. The below code is giving me a null exception..

this is the class that i am calling from the main loop.

class whiteBalloon{
    Sprite whiteballoon;
    SpriteBatch batch;
    OrthographicCamera camera;

    public whiteBalloon(SpriteBatch batch){


    }
    public void create(){
        batch = new SpriteBatch();
        whiteballoon = new Sprite(new Texture("white_balloon.png"));
        whiteballoon.setPosition(0, 0);
        camera = new OrthographicCamera(Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight());
    }

    public void render(){

        System.out.println("hey");

        batch.begin();
        whiteballoon.draw(batch);
        batch.end();



    }

}

and then i call it by using

whiteBalloon grumpface;
        grumpface = new whiteBalloon();
        grumpface.render();

Upvotes: 0

Views: 568

Answers (1)

Ineptus
Ineptus

Reputation: 171

You need to call grumpface.create() before grumpface.render() or just put create() method inside constructor:

public whiteBalloon(){
    create();
}

Also your constructor requires a SpriteBatch object as argument, but you don't provide it later in code, so it uses default (empty) constructor instead of the one you created.

Upvotes: 1

Related Questions