TGB/Tutorials/Fill Battle/Setting Up The Game
From TDN
[edit] Build Your EnvironmentFind where you installed TGB. Copy that directory (it will contain the engine, games, and tgb directories) to a new location and name it FillBattle. I keep my game development in a quick, easy location (“D:\Torque2D”), so I copy to “D:\Torque2D\FillBattle”. NOTE: Do NOT copy this into your “Program Files” directory! Why? (If you don’t care, skip this paragraph.) I always modify the source code and I don’t want multiple changes in every game. Sometimes I add new scene objects, which requires recompiling the TGB Editor. Sometimes I modify base behavior (for example, how a t2dStaticSprite works) which I definitely don’t want everywhere. Keeping everything in its own directory will keep you from contaminating your other projects. Go into FillBattle/tgb and open main.cs. Right at the top is a line “setCompanyAndProject”. Change “TorqueGameBuilder” to “TGBFillBattle”. This will prevent other versions of the TGB Editor from interfering with this version in your %APPDATA% directory. [edit] Build Your SceneIn that same directory, run “TorqueGameBuilder.exe”. ![]()
![]()
![]()
![]()
SixColors.png SquareBorders.png Click on the “Create a New ImageMap” button. Browse to the images directory, select both images, and hit “Open”. ![]()
![]()
Double-click on the SixColors image. Edit the values to turn this into a multi-frame sprite sheet. ![]()
Double-click on the SquareBorders image. Edit the values to turn this into a multi-frame sprite sheet. ![]()
![]()
Edit the layer with the following values: ![]() ![]() ![]()
[edit] Build Your ScriptsLet’s start scripting the game! Open up FillBattle/games/FillBattle/game/gameScripts/game.cs. (NOTE: Except where explicitly mentioned, all directories will be assumed to be under FillBattle/games/FillBattle. For example, in the future, I’ll simply mention game/gameScripts/game.cs.) At the end of the file, insert the following TorqueScript.
function TileBoard::randomFill( %this )
{
// Get the size of the TileBoard.
%width = TileBoard.getTileCountX();
%height = TileBoard.getTileCountY();
// Iterate over all of the tiles.
for( %x = 0; %x < %width; %x++ )
{
for( %y = 0; %y < %height; %y++ )
{
// Pick a random number from 0 to 5 (inclusive).
// This will equal the frame number on the SixColors sprite sheet.
%randomColor = getRandom( 0, 5 );
// Assign the current tile to use the SixColors sprite sheet and
// use the frame that we randomly picked.
TileBoard.setStaticTile( %x, %y, SixColorsImageMap, %randomColor );
}
}
}
Right at the end of the startGame function, we want to call this function. Modify it to look like this:
function startGame(%level)
{
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
TileBoard.randomFill();
}
(Skip this paragraph if you are familiar with TorqueScript.) Remember when we game the TileLayer the name “TileBoard”? Giving an object a name makes it a unique object within the whole scripting system. There can only ever be the one “TileBoard” (in fact, never give something else the same name). We then wrote a function for that specific layer called “randomFill”. The parameter “%this” is equal to the object called “TileBoard”, so we can use either one (for example, “TileBoard.getTileCountX()” could have been “%this.getTileCountX()”). That function, “randomFill”, gets called right after we load this scene. Run the game. You can either go to the FillBattle and double-click “FillBattle.exe” or you can hit the “Play” button on the icon bar of the Torque Game Builder Editor (see below to see what it looks like). Also, if you are using Torsion, you can hit it’s play button. (In the future, I’m just going to say “Run the game.” Choose your method.) ![]()
![]()
[edit] Complete! |

















