Jump to content

  • Log In with Google      Sign In   
  • Create Account


CaseyCoder

Member Since 29 May 2012
Offline Last Active Jun 11 2013 08:59 AM
-----

Topics I've Started

Having trouble loading tiles from a spritesheet (Java android)

31 May 2013 - 04:21 AM

Hi everyone.

 

I am trying to port a simple tile based 2d game from C++ into Java. This is my first go at Java and I'm having problems as this is a brand new IDE/Language/API for me and I think that I'm getting bogged down in details and it's probably my mathematics at fault, but maybe things aren't operating in the same way. Here's what I'm trying to do. The tile sprite sheets have an ID for each tile starting at 1, I need a function that can turn the ID into a x,y location. Each tile is 64 by 64 and the tile sheets are 512 by 512.

I am trying to divide the ID by 8, using the values before the decimal point as the Y location and the decimal numbers as the X location.

This seems to work fine for the first row but it breaks on all other rows.

 

Here is the code:

	public static void draw(int gridX, int gridY, int ID, tileLayer layer){
		int x = gridX * 64;
		int y = gridY * 64;
		
		ID--;

		float sheetPosX = 0;
		float sheetPosY = 0;
		
		float result = (float)(ID / 8f);
		
		sheetPosY = (int)Math.floor(result);
		sheetPosX = result - sheetPosY;
		sheetPosX = (int) (sheetPosX / 0.125f);
		
		System.out.println(sheetPosX + " " + sheetPosY);
		
		Rectangle rect = new Rectangle();
		rect.y = sheetPosY * 64f;
		rect.x = sheetPosX * 64f;
		rect.width = 64f;
		rect.height = 64f;
		
		if (layer == tileLayer.BACKGROUND)
			batch.draw(tileSheet, x - OffsetCamera.offsetX, y - OffsetCamera.offsetY, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
		else if (layer == tileLayer.DOODAD)
			batch.draw(doodadSheet, x - OffsetCamera.offsetX, y - OffsetCamera.offsetY, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
	}

 

Many thanks.

 

Casey


PARTNERS