Game Development Community

Strange Display Results in Tile Map

by Doug Linley · in Torque Game Builder · 04/17/2006 (8:34 pm) · 8 replies

Hey everyone. I created a background image of an 8x8 grid of 64x64 tiles. I then created a tilemap and overlayed that tile map onto the grid image. I tried to write code to place orbs into that tilemap which are just 64x64 images but I'm getting some wierd results. It basically just displays four orbs, one in each corner. They aren't even sized correctly, and when I call setSize(" 64 64") it makes the pieces gigantic.

Here is a shot of what is displaying:

www.cabalgames.com/images/gameshot.jpg
I printed to the console to make sure it was creating all of the pieces and positioning them correctly and that is coming out correct in the console. It is creating 64 orbs, and spacing them 64 pixels apart from each other in both the x and y directions.

Hereis a pic of the console output:

www.cabalgames.com/images/console.jpg

Below I've pasted the code from my game.cs. Thanks in advance for any advice you can give.

#1
04/17/2006 (8:35 pm)
//---------------------------------------------------------------------------------------------
// Torque 2D. 
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
   // Set The GUI.
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   
   moveMap.push();
   sceneWindow2D.loadLevel(%level);
   
   new ScriptClass(gridObj);
   
   $gridObj.init();
}

//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
   sceneWindow2D.endLevel();
   moveMap.pop();
}

function gridObj::onAdd(%this)
{
   $gridObj = %this;   
}


function gridObj::init(%this)
{
   // Make the checkerboard
   %this.tileMap = new t2dTileMap() { scenegraph = t2dScene; };

   // 8x8 tile layer, each tile is 64x64
	  %this.gridLayer = %this.tileMap.createTileLayer( "8 8 64 64" );
	  
	  
   // The board needs to be centered
   %this.gridLayer.setPosition("0 0");
   
   // The board itself is 512 (8 * 64)
	  %this.gridLayer.setSize( "512 512" );
	  
	  %this.gems = new SimSet();
	  
   
   // This is render-layer 30, which is way to the back, it will also let us
   // do mouse-event checks for only layer 30
	  %this.gridLayer.setLayer( 30 );
	  
	  for(%x = 0; %x < 8; %x++)
	  {
      for(%y = 0; %y < 8; %y++)
      {
         %this.setGem(%x, %y);
      }
	  }
}

function gridObj::setGem(%this, %x, %y)
{
   %num = %this.gems.getCount();
   
   echo("setting gem" @ %num);
   
   %gem = new t2dStaticSprite("gem" @ %num) {sceneGraph = t2dScene; };
   %gem.setLayer($layers::gemLayer);
   %gem.setImageMap(GreenChip);  
   
   %this.gems.add(%gem); 
   
   %pos = %this.getTilePosition(%x, %y);
   
   echo("pos: " @ %pos);
   
   %gem.setPosition(%pos);
   
   %this.images[%x, %y] = %gem;
   
   %this.board[%x, %y] = $greenChip;
}

function gridObj::getTilePosition(%this, %x, %y)
{
   // grab the board layer's position
   %tileMapPos = %this.gridLayer.getPosition();
   
   // divide the position up into x and y variables
   %tileMapPosX = getWord(%tileMapPos, 0);
   %tileMapPosY = getWord(%tileMapPos, 1);
   
   // grab the board layer's size
   %tileMapSize = %this.gridLayer.getSize();
   
   // divide the size up into x and y variables
   %tileMapSizeX = getWord(%tileMapSize, 0);
   %tilemapSizeY = getWord(%tileMapSize, 1);
   
   // calculate the start position
   %tileMapStartX = %tileMapPosX - (%tileMapSizeX / 2);
   %tileMapStartY = %tileMapPosY - (%tileMapSizeY / 2);
   
   // calculate the position and pass it back
   %pos = (%tileMapStartX + (%x * 64)) + 32 SPC (%tileMapStartY + (%y * 64)) + 32;

   return %pos;   
}
#2
04/17/2006 (11:41 pm)
I'm not sure if this is the problem, but is you're camera size 100 x 75 ?? (the default I think)

it would appear like this in script: setCurrentCameraPosition( 0, 0, 100, 75 )

or possibly setCurrentCameraPosition(%pos SPC %size) , in InitializeT2D.cs


try setting it to 0,0,640,480 or whatever the default resolution is that you're designing in so that things get sized and positioned relative to that, the 100, 75 thing messes with my head.
#3
04/18/2006 (5:00 am)
Thanks for the reply. I have the resolution set to the same resolution I am designing in, and the camera position is 0, 0, 100, 75.
#4
04/18/2006 (6:25 am)
I believe your problem is related to those hardcoded numbers in your getTilePosition function.

Try replacing the 64 with %this.gridlayer.getTileSizeX() and the 32 with %this.gridlayer.getTileSizeX()/2
(using getTileSizeY() for the Y coords, of course)

EDIT: I also think "%tileMapStartX = %tileMapPosX - (%tileMapSizeX / 2);" (and the equivalent line for Y) should actually be addition, not subtraction, but I haven't looked at it too closely

EDIT 2: You can also set the size of your green gems to:
%this.gridlayer.getTileSizeX() SPC %this.gridlayer.getTileSizeY()
#5
04/18/2006 (6:44 am)
Thanks Drew. I'll give this a try. The code you suggest changing, I basically just ripped out of the checkers demo, which is doing basically the exact same thing I am. I'll make those changes this evening and see what I get.
#6
04/18/2006 (6:56 am)
Doug, you've missed the point of what Ezra was saying.

Try changing your camera to (0,0,640,480) anyway.

It's gets a bit complicated, but running at 0,0,100,75 you will probably find that you need to divide everything by 6.4. (640/6.4)=100. (480/6.4)=75.

So that means to have your images sized at 64x64 pixels, you need to set the size as "10 10".

TGB seperates the pixel resolution from the screen coordinates which is great for supporting multiple resolutions with minimum extra work, but you do need to keep it mind when setting positions and sizes for your objects.
#7
04/18/2006 (7:17 am)
Oh! Thanks Philip. I get it now. I'm sorry Ezra. I'm used to working with 2d in a more conventional way, and what you were saying went right over my head. Thanks again!
#8
04/18/2006 (3:00 pm)
The camera setting was definitely the problem. It's working beautifully now! Thanks to everyone for your help.