Torque 2D/GenreTutorials/PlatformerGUI
From TDN
[edit] IntroductionIn this tutorial, we will be replacing the in game interface so that it includes a life bar and shows the number of remaining lives. We will also create a menu, a level select dialog, and a game over screen.
|
|
[edit] In Game InterfaceFirst of all, you are going to need two image files for the life bar. One of them will be the outline, and one the fill. Here are the very simple images I made. Either right click on each of these and save them as lifeBar.png and lifeBarOutline.png to the T2D/data/images directory, or create your own with the same names.
exec(“~/data/gui/gameHUD.guiâ€); Also in the client.cs file, change the line Canvas.setContent(mainScreenGUI); to Canvas.setContent(gameHUDGui); Now open player.cs and add this line to the resetPlayer function: LifeBar.extent = LifeBarOutline.extent; This is resetting the size of the life bar to equal the size of the life bar outline, effectively displaying full life. Updating the length of the life bar when the player gets hit is similar. Add this to the end of the playerHit function: %newX = getWord(LifeBarOutline.extent, 0) * ($player.life / 100); LifeBar.extent = %newX SPC getWord(LifeBar.extent, 1); This will set the length of the life bar proportionately to the length of the life bar outline based on the amount of life the player currently has. |
|
[edit] Menu ScreensThe first thing we need to do is restructure the client.cs file. As it is now, the in game interface is the first gui that is shown. We need to change that. Open client.cs and change these three lines: Canvas.setContent(gameHUDGui); Canvas.setCursor(DefaultCursor); setupT2DScene(); to this: loadT2DIntro(); This new function will eventually set the T2D splash screen. For now, we are going to use it to load the main menu. We will define it after we create the actual gui. Before we do that, let’s finish off the client.cs changes. We are going to need exec statements for each of our GUIs. Here they are:
exec("~/data/gui/t2dIntro.gui");
exec("~/data/gui/mainMenu.gui");
exec("~/data/gui/levelSelect.gui");
exec("~/data/gui/levelIntro.gui");
exec("~/data/gui/gameOver.gui");
We will also be creating a new file called gui.cs that manages all the GUI transitions and such. Add its exec statement as well:
exec("./gui.cs");
Since we aren’t going to be starting the actual game right at engine startup, we need start game and end game functions and we can get rid of the setupT2DScene function. Here are the functions and a variable declaration:
$gameStarted = false;
function startGame()
{
if ($gameStarted)
return;
$gameStarted = true;
new t2dSceneGraph(t2dScene);
sceneWindow2D.setSceneGraph(t2dScene);
sceneWindow2D.setCurrentCameraPosition("0 0 100 75");
createPlayer();
createCamera();
}
function endGame()
{
$gameStarted = false;
if (isObject($camera))
$camera.delete();
if (isObject($player))
$player.delete();
if (isObject(t2dScene))
t2dScene.delete();
}
The $gameStarted variable just tracks whether or not the actual game has started (not just the menus). The first thing we do in startGame is make sure the game isn’t already started. Starting it again would make no sense. The rest is basically the same as setupT2DScene.
function destroyClient()
{
endGame();
}
Now create the gui.cs file and add these functions to it:
function loadT2DIntro()
{
Canvas.setCursor(DefaultCursor);
loadMainMenu();
}
function loadMainMenu()
{
Canvas.setContent(MainMenuGUI);
}
For now, loadT2DIntro is just passing things on to loadMainMenu, which sets the MainMenuGui that we are now creating.
function loadFirstLevel()
{
startGame();
loadLevel("~/data/levels/testLevel.cs");
}
Go ahead and run the game to make sure everything is as expected. The quit button should quit the game, the options button will open the T2D default options dialog, and the play button will start the game. Now let’s hook up the level select dialog. |
|
[edit] Level Selection DialogThe level select dialog will probably not make it into your final game, but it can be very useful for testing by letting you access any level at any time. Most of the functionality will be provided with code, but we do need a simple gui to interact with, so open up the GUI editor again.
function LevelSelectGui::onWake()
{
%levelSpec = "~/data/levels/*.cs";
LevelSelectList.clear();
%row = 0;
for(%file = findFirstFile(%levelSpec);
%file !$= ""; %file = findNextFile(%levelSpec))
LevelSelectList.addRow(%row++, fileBase(%file) TAB %file);
LevelSelectList.sort(0);
LevelSelectList.setSelectedRow(0);
LevelSelectList.scrollVisible(0);
}
function loadSelectedLevel()
{
%id = LevelSelectList.getSelectedId();
%level = getWord(LevelSelectList.getRowTextById(%id), 1);
startGame();
loadLevel(%level);
}
The onWake function will be called automatically by the engine anytime the LevelSelectGui is activated (i.e. by the Canvas.pushDialog function). Its purpose is to populate the level list with all of the levels in the game. The levels are found by scanning the levels folder. First we set up the file spec which we will use to tell certain functions which files we want to know about (cs files in the levels folder). Then we clear the list in case we are setting the gui a second time. The for loop just loops through every file that matches our file spec and adds it to our LevelSelectList.
|
|
[edit] Splash ScreensAny game you release with T2D requires that you have a T2D logo in the startup sequence. We will now create one of these, as well as a game over screen. These are both extremely simple, consisting of one gui control each. First the T2D splash screen.
function loadT2DIntro()
{
Canvas.setCursor(DefaultCursor);
Canvas.setContent(T2DIntroGui);
$GUISchedule = schedule(3250, 0, "loadMainMenu");
}
With this function, we are setting the T2DIntroGui and scheduling the main menu to load in 3.25 seconds. The schedule is being assigned to $GUISchedule so it can be canceled by this:
function T2DIntroGui::click()
{
cancel($GUISchedule);
loadMainMenu();
}
This just allows us to end the splash screen early with a mouse click or keypress.
function GameOverGui::click()
{
cancel($GUISchedule);
loadMainMenu();
}
And to set this gui, change this line in the playerDie function of player.cs:
echo(“Game Overâ€):
to:
schedule(1000, 0, "gameOver");
And add this function to level.cs:
function gameOver()
{
Canvas.setContent(GameOverGui);
$GUISchedule = schedule(3250, 0, "loadMainMenu");
endLevel();
endGame();
}
That should be enough to get you started creating all sorts of GUIs for your games. T2D’s GUI system is extremely powerful and this tutorial barely scratched the surface of what it can do. There are a bunch of other tutorials and such to help you in this area, and you should be able to experiment on your own to figure out more and make things look nicer.
|
Categories: T2D | Getting Started | Platformer | GUI | Tutorial



