Using libGdx, nothing is shown after calling stage.draw() which stage has extendViewport

23 Views Asked by At

game screen is:

public class GameScreen implements Screen {
    final Test1 game;
    
    private Stage stage;
    private TrafficGame trafficGame;
    
    public GameScreen(Test1 _game) {
        game = _game;
        stage = new Stage(new ExtendViewport(800, 480));

        trafficGame = new TrafficGame();
        stage.addActor(trafficGame);
        
    }

        public void render(float delta) {
        Gdx.gl.glClearColor(255, 255, 255, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        // calls TrafficGame actor act/draw method
        stage.act(delta);
        stage.draw();
    }

and TrafficGame which has main logic:

public class TrafficGame extends Table {
    private InfiniteScrollBg backgroundRoad;
    
    public TrafficGame() {
        setBounds(0, 0, 800, 480);
        backgroundRoad = new InfiniteScrollBg(getWidth(), getHeight());
        addActor(backgroundRoad);
    }
    
    public void act(float delta) {
        super.act(delta);
    }

    public void draw(SpriteBatch batch, float parentAlpha) {
        batch.setColor(Color.WHITE);
        super.draw(batch, parentAlpha);
    }   

}

and InfiniteScrollBg which is actor drawing scrolling road:


public class InfiniteScrollBg extends Actor {
    private static Texture road;
    
    {
        road = new Texture(Gdx.files.internal("road.png"));
    }

    public InfiniteScrollBg(float width, float height) {
        setWidth(width);
        setHeight(height);
        setPosition(width, 0);
                addAction(forever(sequence(moveTo(0, 0, 1f), moveTo(width, 0))));
    }

    public void draw(SpriteBatch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        batch.draw(road, getX() - getWidth(), getY(), getWidth() * 2, getHeight());
    }
}

changed InfiniteScrollBg.draw() parameters: from batch.draw(road, getX() - getWidth(), getY(), getWidth() * 2, getHeight()); to batch.draw(road, 0, getY(), getWidth() * 2, getHeight());

or moving stage.camera to (0, 0)

or anything I try not solving problem...

0

There are 0 best solutions below