Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - Displaying a texture using the coordinates of an object layer #7330

Open
2 of 6 tasks
BREBION-Mathis opened this issue Jan 31, 2024 · 0 comments
Open
2 of 6 tasks

Comments

@BREBION-Mathis
Copy link

Hi, I'm developing a 2D game using a Tilemap built with Tiled. The Tilemap display is fine, but I would like to create water animations at two points on the map by fetching the object layer coordinates from the tmx file (my tilemap file). I manage to get the object, its co-ordinates and the size of the object, but when it comes to displaying the texture, the texture is not displayed at all, without any error message.

Reproduction steps/code

private static final int TILESET_COLS = 26, TILESET_ROWS = 15, TILE_WIDTH = 32, TILE_HEIGHT = 32;

	private AssetManager assetManager;

	private TiledMap map;

	private Texture tileSheet;

	private OrthogonalTiledMapRenderer tilemapRenderer;

	// Camera
	private OrthographicCamera camera;

	private SpriteBatch batch;
	private BitmapFont font;

	// Animation des chûtes d'eau
	private Animation<TextureRegion> waterFallAnimation;
	private List<Rectangle> waterFallRectangles = new LinkedList<Rectangle>();
	private float _waterFallStateTime = 0;

	@Override
	public void create() {
		batch = new SpriteBatch();

		assetManager = new AssetManager();
		assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
		assetManager.load("Map.tmx", TiledMap.class);
		assetManager.finishLoading();

		map = assetManager.get("Map.tmx");

		// Rendu de la map
		tilemapRenderer = new OrthogonalTiledMapRenderer(map);

		// Mettre la fenêtre en mode plein écran
		Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());

		// Mise en place de la caméra dans la scène
		camera = new OrthographicCamera();
		camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.update();

		// Rendre le fenêtre redimensionnable
		Gdx.graphics.setResizable(true);

		// Chargement de l'animation des chûtes d'eau
		tileSheet = new Texture(Gdx.files.internal("Tiles/32x32.png"));

		TextureRegion[][] waterFallTmp = TextureRegion.split(tileSheet, TILE_WIDTH, TILE_HEIGHT);
		TextureRegion[] waterFallFrames = new TextureRegion[3];

		waterFallFrames[0] = waterFallTmp[10][14];
		waterFallFrames[1] = waterFallTmp[10][15];
		waterFallFrames[2] = waterFallTmp[10][16];

		waterFallAnimation = new Animation<TextureRegion>(0.150f, waterFallFrames);

		// Récupération du groupe d'object Layers
		MapGroupLayer objectsGroupLayer = (MapGroupLayer) map.getLayers().get("ObjectsLayers");

		// Récupération de l'object Layer "Waterfall"
		MapLayer waterFallLayer = objectsGroupLayer.getLayers().get("Waterfall");

		// Récupération des objets de l'object Layer "Waterfall"
		MapObjects waterFallObjects = waterFallLayer.getObjects();

		for (MapObject object : waterFallObjects) {
			RectangleMapObject rectangleMapObject = (RectangleMapObject) object;
			waterFallRectangles.add(rectangleMapObject.getRectangle());
		}
	}

	@Override
	public void render() {
		ScreenUtils.clear(0, 0, 0, 1);

		camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.update();

		tilemapRenderer.setView(camera);
		tilemapRenderer.render();

		// Gestion de l'animation des chûtes d'eau
		_waterFallStateTime += Gdx.graphics.getDeltaTime();

		TextureRegion currentFrame = waterFallAnimation.getKeyFrame(_waterFallStateTime, true);

		batch.begin();

		for (Rectangle rectangle : waterFallRectangles)
			batch.draw(currentFrame, rectangle.x, rectangle.y, rectangle.width, rectangle.height);

		batch.end();
	}

Version of libGDX and/or relevant dependencies

Latest

Please select the affected platforms

  • Android
  • iOS
  • HTML/GWT
  • Windows
  • Linux
  • macOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant