[libgdx] Problems with stage

Hi guys, I was reading Your posts for a very long time, but now i have a problem which i can’t figure out.

I have GameScreen (parent), GameWorld (logic) and GameRenderer (vision, rendering). So, when i Was trying to render my game normally (renderer was taking actors and just painting them on screen) everything was fine. Then I want to use Stage and Actors (for simply have a more clearly code and have better input logic) and i spot some problems. In fact - my map isn’t drawing at all - simple array[][] and my stage only covers~3/7 (in top-right corner) of my desired window.

My Actor (Hero) is covering whole stage, but it is drawed outsde it (in bottom-left corner).

If someoe want to help me, I’ll be gratefull - I can provide some code of mine (there is not much of it, I’ve just started), just tell what.

I’m really stuck with it.

Red color is my stage, the blue one is my hero (rendered - because when i click in bottom-left corner of my stage he reacts -> moving x += 40, y += 52) I can’t click him again)

Maybe some code would help:

GameScreen.java

public class GameScreen implements Screen, GestureListener{

	private GameWorld world;
	private GameRenderer renderer;
	private Stage stage;
	
	private float runTime = 0;
	private FPSLogger fpsLogger;
	
//	private final int WIDTH = Gdx.graphics.getWidth();
//	private final int HEIGHT = Gdx.graphics.getHeight();
	
	public GameScreen() {
		stage = new Stage();
		world = new GameWorld(stage);
		Gdx.input.setInputProcessor(world.getStage());
		fpsLogger = new FPSLogger();
		renderer = new GameRenderer(world);
	}

GameWorld.java

public class GameWorld {
	
	//zmienne
	private Hero hero;
	private GameMap currentMap;
	
	private Stage stage;
	
	private GameStateEnum currentState = GameStateEnum.RUNNING;
	
	//constructor
	public GameWorld(Stage stage) {
		
		this.stage = stage;
		
		initializeGameWorld();
	}

	private void initializeGameWorld() {
		assetsInit();
		hero = new Hero();
		currentMap = new GameMap(2);
		
		stage.addActor(currentMap);
		stage.addActor(hero);
	}

	public void update(float delta) {
		stage.act(delta);
	}
	
	private void assetsInit() {
		AssetLoader.load();
	}

And problem is still the same: Stage is always ALWAYS half of the width and height. So when i try to test it on my desktop project with 800x600 resolution - This give me stage of 400x300 p, but stage.getWidth() giving me 800, so do stage.getHeight() - 600.
When i try to click in the middle of desktop game there pop up a message from InputListener.touchDown that x and y are even to 0.

Could You have any ideas what to do?

Welcome to the forum, Lidjan.

I’m pretty new to using LibGDX and stage myself, would I be able to get a look at your render code to take a closer look. I’m looking over the code you have supplied right now to see if I can see the issue there

GameRenderer.java just drawing actors:

public class GameRenderer {
	
	private GameWorld world;
	private SpriteBatch batcher;
	private OrthographicCamera cam;
	
	//constructor
	public GameRenderer(GameWorld world) {
		this.world = world;
		this.cam = new OrthographicCamera();
		
		batcher = world.getStage().getSpriteBatch();
		batcher.setProjectionMatrix(cam.combined);
	}
	
	public void render(float delta) {
		
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batcher.begin();
		
		for( Actor a : world.getStage().getActors()) {
			a.draw(batcher, 1);
		}
		
		batcher.end();
	}
}

GameMap.java draw method:

@Override
	public void draw(SpriteBatch batcher, float parentAlpha) {

		batcher.disableBlending();
		for (int i = mapPositionX; i < MAP_SIZE; i++) {
			for (int j = mapPositionY; j < MAP_SIZE; j++) {
				if (tiles[i][j] != null && !tiles[i][j].isBlocking()) {
					
					batcher.draw(tiles[i][j].getTileTexture(), (i - mapPositionX) * AssetLoader.MAP_TILESET_WIDTH, 
							(j - mapPositionY) * AssetLoader.MAP_TILESET_HEIGHT, AssetLoader.MAP_TILESET_WIDTH, 
							AssetLoader.MAP_TILESET_HEIGHT);
				} else {
					batcher.draw(new Tile(TileEnum.GRASS, true).getTileTexture(), 
							(i - mapPositionX) * AssetLoader.MAP_TILESET_WIDTH, 
							(j - mapPositionY) * AssetLoader.MAP_TILESET_HEIGHT, AssetLoader.MAP_TILESET_WIDTH, 
							AssetLoader.MAP_TILESET_HEIGHT);
				}
				
				AssetLoader.newFont.draw(batcher, "" + ((i * 100) + j) , 
						(i - mapPositionX) * AssetLoader.MAP_TILESET_WIDTH, 
						(j - mapPositionY) * AssetLoader.MAP_TILESET_HEIGHT);
			}
		}
		
	}

Here is some code which will be refactored later - when some things start working properly :wink:

I noticed a few things that you could try before looking into the issue further. I noticed that you never set your camera up with setToOrtho()

As well as this, I noticed you are grabbing the Stage spritebatch, however Stage can be drawn with stage.draw(); and doesn’t need to be inside a spritebatch. I’m honestly not sure if this could cause conflicting issues but it might be worth a look?

I also can’t see any code that relates to the position of the actors on the screen, could this have anything to do with it? I noticed you said everything was working fine until you used the stage and actors, could you expand on this a little? :slight_smile:

EDIT: Also, you are passing in your map as an actor and this might be causing an issue. Stage is usually used with scene2D with are useful for creating things like buttons, text fields and sliders so I think this might be your issue. Try rendering the map using a spritebatch instead(if you are not using a Tiled map). If you are using a Tiled map then you should use the following