Quit Button, CountOnDeath Behaviour & Display Lives Behaviour
by Ryan Dixon · in Torque Game Builder · 03/09/2010 (10:03 am) · 4 replies
I am currently creating my first 'game', but i have some problems with it and need some advice please.
1. How to put a 'quit' buttin in a main menu. Is there a behaviour available that i can attach to the text, like there is for the Level Button?
2. For the display lives, how to make one of the objects disappear when my ship explodes?
3. How to display a score when my enemy ship is destroyed using the count on death behaviour?
*****************CODING FOR DISPLAY LIVES BEHAVIOUR***************************************
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Behavior by Mike Lilligreen for TDN Asteroids Tutorial
//-----------------------------------------------------------------------------
if (!isObject(DisplayLivesBehavior))
{
%template = new BehaviorTemplate(DisplayLivesBehavior);
%template.friendlyName = "Display Lives";
%template.behaviorType = "GUI";
%template.description = "Displays the number of player lives";
%template.addBehaviorField(object, "The object to clone as an icon", object, "", t2dSceneObject);
%template.addBehaviorField(height, "Height of the icon", float, 3.0);
%template.addBehaviorField(width, "Width of the icon", float, 3.0);
%template.addBehaviorField(lives, "The number of lives the player has (for GUI display only!)", int, 5);
}
function DisplayLivesBehavior::onAddToScene(%this, %scenegraph)
{
new SimSet(LifeIcons);
$currentLife = %this.lives;
%this.spawnCount = 0;
%this.schedule(100, "spawn");
}
function DisplayLivesBehavior::onRemoveFromScene(%this, %scenegraph)
{
LifeIcons.delete();
}
function DisplayLivesBehavior::spawn(%this)
{
if (!isObject(%this.object))
return;
%clone = %this.object.clone();
%modifier = %this.spawnCount * %this.width;
%xPos = getWord(%this.owner.getAreaMin(), 0) + %modifier;
%yPos = %this.owner.position.y;
%clone.position = %xPos SPC %yPos;
%clone.size = %this.height SPC %this.width;
LifeIcons.add(%clone);
%this.spawnCount++;
if (%this.spawnCount < $currentLife)
{
%this.spawn();
}
}
function DisplayLivesBehavior::update(%this)
{
%count = LifeIcons.getCount();
%lastIcon = LifeIcons.getObject(%count - 1);
LifeIcons.remove(%lastIcon);
%lastIcon.safeDelete();
$currentLife--;
}
*************************CODING FOR COUNT ON DEATH BEHAVIOUR**********************************
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(CountOnDeathBehavior))
{
%template = new BehaviorTemplate(CountOnDeathBehavior);
%template.friendlyName = "CountOnDeath";
%template.behaviorType = "Game";
%template.description = "When object dies, changes a counter value (settable)";
%template.addBehaviorField(count, "The number to modify the value by (-ve is ok)", int, 100);
%template.addBehaviorField(counter, "The counter object", object, "", t2dSceneObject);
}
// basically, when the object this behavior is attached to dies
// this behavior looks at the counter object and tries to modify
// any counter behavior attached to it.
function CountOnDeathBehavior::onRemoveFromScene(%this, %scenegraph)
{
if (!isObject(%this.counter))
return;
%counter = %this.counter.getBehavior("CounterBehavior");
if (!isObject(%counter))
return;
%counter.ModifyCount(%this.count);
}
1. How to put a 'quit' buttin in a main menu. Is there a behaviour available that i can attach to the text, like there is for the Level Button?
2. For the display lives, how to make one of the objects disappear when my ship explodes?
3. How to display a score when my enemy ship is destroyed using the count on death behaviour?
*****************CODING FOR DISPLAY LIVES BEHAVIOUR***************************************
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Behavior by Mike Lilligreen for TDN Asteroids Tutorial
//-----------------------------------------------------------------------------
if (!isObject(DisplayLivesBehavior))
{
%template = new BehaviorTemplate(DisplayLivesBehavior);
%template.friendlyName = "Display Lives";
%template.behaviorType = "GUI";
%template.description = "Displays the number of player lives";
%template.addBehaviorField(object, "The object to clone as an icon", object, "", t2dSceneObject);
%template.addBehaviorField(height, "Height of the icon", float, 3.0);
%template.addBehaviorField(width, "Width of the icon", float, 3.0);
%template.addBehaviorField(lives, "The number of lives the player has (for GUI display only!)", int, 5);
}
function DisplayLivesBehavior::onAddToScene(%this, %scenegraph)
{
new SimSet(LifeIcons);
$currentLife = %this.lives;
%this.spawnCount = 0;
%this.schedule(100, "spawn");
}
function DisplayLivesBehavior::onRemoveFromScene(%this, %scenegraph)
{
LifeIcons.delete();
}
function DisplayLivesBehavior::spawn(%this)
{
if (!isObject(%this.object))
return;
%clone = %this.object.clone();
%modifier = %this.spawnCount * %this.width;
%xPos = getWord(%this.owner.getAreaMin(), 0) + %modifier;
%yPos = %this.owner.position.y;
%clone.position = %xPos SPC %yPos;
%clone.size = %this.height SPC %this.width;
LifeIcons.add(%clone);
%this.spawnCount++;
if (%this.spawnCount < $currentLife)
{
%this.spawn();
}
}
function DisplayLivesBehavior::update(%this)
{
%count = LifeIcons.getCount();
%lastIcon = LifeIcons.getObject(%count - 1);
LifeIcons.remove(%lastIcon);
%lastIcon.safeDelete();
$currentLife--;
}
*************************CODING FOR COUNT ON DEATH BEHAVIOUR**********************************
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(CountOnDeathBehavior))
{
%template = new BehaviorTemplate(CountOnDeathBehavior);
%template.friendlyName = "CountOnDeath";
%template.behaviorType = "Game";
%template.description = "When object dies, changes a counter value (settable)";
%template.addBehaviorField(count, "The number to modify the value by (-ve is ok)", int, 100);
%template.addBehaviorField(counter, "The counter object", object, "", t2dSceneObject);
}
// basically, when the object this behavior is attached to dies
// this behavior looks at the counter object and tries to modify
// any counter behavior attached to it.
function CountOnDeathBehavior::onRemoveFromScene(%this, %scenegraph)
{
if (!isObject(%this.counter))
return;
%counter = %this.counter.getBehavior("CounterBehavior");
if (!isObject(%counter))
return;
%counter.ModifyCount(%this.count);
}
About the author
#2
04/06/2010 (9:50 am)
Hi Mark, i now have a new problem which relates to the scoring system, in the fact that it doesn't work! Any ideas?
#3
04/06/2010 (2:51 pm)
Hello Ryan, following the tutorial step by step should give you a working scoring system. Did you attach the behaviors to each object? Is the counter object named and assigned in the counter field of the score object behavior?
#4
04/07/2010 (6:57 am)
Yes. I tcomes up as "Score:0" but then doesnt do anything else. Is there another way i could implement a scoring sytem? Perhaps using a GUI?
Associate Mike Lilligreen
Retired T2Der