Game Development Community

How to make 4 layers work?

by Matthews_30 · in Torque Game Builder · 11/28/2005 (12:21 pm) · 3 replies

I need to make 3 layers works, the scenario is the following.

1. mountains in the background, fixed, static

2. 4 clouds moving slowly

3. a river faster than the clouds

4. my character and the enemies

i was trying to understand this with the platfformer tutorial but i am not understanding.

is there somebody who can explain how to make this layers to work?

thanks a lot.

#1
11/28/2005 (12:48 pm)
This is how I setup multiple scrolling backgrounds in T2D 1.02:
function initTileMap()
{
	// define our tile map
	%levelMap = new fxTileMap2D() { scenegraph = t2dSceneGraph; };
	%levelMap.loadTileMap("~/client/maps/level1.map");

	// assign the background tile layers to variables
	%backLayer1 = %levelMap.getTileLayer( 0 );
	%backLayer2 = %levelMap.getTileLayer( 1 );
	%backLayer3 = %levelMap.getTileLayer( 2 );
	%backLayer4 = %levelMap.getTileLayer( 3 );

	// assign the tile layers to world layers, and get them moving
	%backLayer1.setPosition( "0 0" ); 
	%backLayer1.setLayer(30);
	%backLayer1.setWrap(true, false);
	%backLayer1.setAutoPan("5 0");

	%backLayer2.setPosition( "0 0" ); 
	%backLayer2.setLayer(29);
	%backLayer2.setWrap(true, false);
	%backLayer2.setAutoPan("8 0");

	// this bit of code works out how wide a tile layer is to ensure the left edge of the layer
	// is against the left edge of the camera view
	%backLayer3TileCountX=GetWord(%backLayer3.getTileCount(),0);
	%backLayer3TileSizeX=GetWord(%backLayer3.getTileSize(),0);
	%currentCameraviewWidth=GetWord(sceneWindow2D.getCurrentCameraArea(),2);

	%backLayer3StartPosX=(((%backLayer3TileCountX * %backLayer3TileSizeX) / 2) - %currentCameraviewWidth /2);

	%backLayer3.setPosition( %backLayer3StartPosX SPC "0" ); 
	%backLayer3.setLayer(25);
	%backLayer3.setWrap(true, false);
	%backLayer3.setAutoPan("12 0");

	%backLayer4TileCountX=GetWord(%backLayer4.getTileCount(),0);
	%backLayer4TileSizeX=GetWord(%backLayer4.getTileSize(),0);

	%backLayer4StartPosX=(((%backLayer4TileCountX * %backLayer4TileSizeX) / 2) - %currentCameraviewWidth /2);

	%backLayer4.setPosition( %backLayer4StartPosX SPC "0" ); 
	%backLayer4.setLayer(10);
	%backLayer4.setWrap(true, false);
	%backLayer4.setAutoPan("20 0");
}
This will give you 4 tile layers all scrolling at different speeds. You should be able to modify it for your specific needs.
#2
11/29/2005 (7:17 am)
Also, don't forget that in the alpha-release you can now use...

"%obj.setArea()"

... to set the area of any object. This can be especially useful for tile-layers.

- Melv.
#3
02/17/2006 (7:59 am)
I arrived at this a different way, so I'll post it just to give you another option. My approach is similar to the above, but I can have as many or as few background layers as I want in a tilemap and they'll all scroll correctly.

First, I set up my tilemaps so that layers 0 through n are "backgrounds". n+1 is my "platform" layer, with the blocks that the player actually lands on. n+2 is the "special" layer, which I use for enemy placement, spawnpoint, clear point, etc. In short, the top two layers scroll normally, the layers below that, no matter how many, will each move slower than the one in front of it by half.

So when I load a level, I grab the tilelayers like so:

TileMap.loadTileMap(%map);
  
  %layercount = TileMap.getTileLayerCount();
  
  for(%t = %layercount; %t > -1; %t--){
    switch(%t){
     case %layercount - 1:
      $sg = TileMap.getTileLayer(%t);
     case %layercount - 2: 
      $fg = TileMap.getTileLayer(%t);
     default:
      %bg = TileMap.getTileLayer(%t);
      BackgroundGroup.add(%bg);
    }
  }
  
  for (%i = 0; %i < BackgroundGroup.getCount(); %i++)
  {
    %bg = BackgroundGroup.getObject(%i);
    %bg.setPosition("0 0");
    %bg.setLayer($bg_LR);
  }

Then, in onUpdateScene, I do this:

%position = sceneWindow2d.getCurrentCameraArea();
    %positionx = getWord(%position, 0);
    %positiony = getWord(%position, 1);
     
     for (%i = 0; %i < BackgroundGroup.getCount(); %i++)
     {
       %bg = BackgroundGroup.getObject(%i);
       %x0 = (%positionx / ((%i+1) * 2)) - %positionx;
       %y0 = (%positiony / ((%i+1) * 2)) - %positiony;
       %bg.setPanPosition(%x0 SPC %y0);
     }

(This was written for beta 1)