TGB/Tutorials/Asteroids/Section6
From TDN
[edit] Asteroids TutorialWritten for TGB Version: 1.6 |
|
[edit] Starting with Behaviors
function SpawnAreaBehavior::messageTextObject(%this)
{
%this.textObject.displayLevelText();
}
%template.addBehaviorField(textObject, "The object which displays level text", object, "", t2dSceneObject);
%this.schedule(%this.spawnTime * 100, "messageTextObject");
%this.schedule(%this.spawnTime * 1000, "messageTextObject");
|
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Behavior by Mike Lilligreen for TDN Asteroids Tutorial
//-----------------------------------------------------------------------------
if (!isObject(GameTextBehavior))
{
%template = new BehaviorTemplate(GameTextBehavior);
%template.friendlyName = "Game Text";
%template.behaviorType = "GUI";
%template.description = "Displays text between levels, game over, etc.";
%template.addBehaviorField(fadeIncrement, "The smoothness of the text fade in/out", int, 30);
%template.addBehaviorField(fadeLength, "The length of time in seconds the text should fade", float, 2.0);
%template.addBehaviorField(displayLength, "The length of time in seconds the text should be displayed", float, 1.0);
}
function GameTextBehavior::onAddToScene(%this)
{
%this.levelNumber = 0;
%this.startPosition = %this.owner.getPosition();
%this.fadeLength = %this.fadeLength * 1000;
%this.displayLength = %this.displayLength * 1000;
}
function GameTextBehavior::displayLevelText(%this)
{
%this.levelNumber = %this.levelNumber++;
%this.owner.text = "Level" SPC %this.levelNumber;
%this.owner.setBlendAlpha(0);
%this.owner.setPosition(0, 0);
%this.fadeInText(1, %this.fadeLength, 1);
}
function GameTextBehavior::fadeInText(%this, %toAlpha, %time, %toggle)
{
if(%time > %this.fadeIncrement)
{
%alpha = %this.owner.getBlendAlpha();
%updatesRemaining = %time / %this.fadeIncrement;
%alpha += (%toAlpha - %alpha) / %updatesRemaining;
%this.owner.setBlendAlpha(%alpha);
%this.schedule(%this.fadeIncrement, "fadeInText", %toAlpha, %time - %this.fadeIncrement, %toggle);
}else
{
%this.owner.setBlendAlpha(%toAlpha);
if (%toggle == 1)
%this.schedule(%this.displayLength, "removeLevelText");
}
}
function GameTextBehavior::removeLevelText(%this)
{
%this.fadeOutText(0, %this.fadeLength);
}
function GameTextBehavior::fadeOutText(%this, %toAlpha, %time)
{
if(%time > %this.fadeIncrement)
{
%alpha = %this.owner.getBlendAlpha();
%updatesRemaining = %time / %this.fadeIncrement;
%alpha += (%toAlpha - %alpha) / %updatesRemaining;
%this.owner.setBlendAlpha(%alpha);
%this.schedule(%this.fadeIncrement, "fadeOutText", %toAlpha, %time - %this.fadeIncrement);
}else
{
%this.owner.setBlendAlpha(%toAlpha);
%this.owner.setPosition(%this.startPosition);
}
}
function GameTextBehavior::gameOverText(%this)
{
%this.owner.text = "Game Over";
%this.owner.setBlendAlpha(0);
%this.owner.setPosition(0, 0);
%this.fadeInText(1, %this.fadeLength, 1);
}
|
[edit] Back in the TGB Editor
|







