Making a Clock (page 2)

 Adding buttons

To adjust the speed of time we will use the time multipliers. Place a 1X multiplier image to the right of the clock. Under the scripting tab change its class name to Multiply1X. We will use this class to separate the other multipliers. Also check the use mouse events checkbox so we can click the object.

 

Coding our buttons:

To program when an object is clicked we use the following code. We will simply replace the ClassName with the class of our buttons. It is a good thing to note that the %this argument is the objectId of the object clicked.

Template: Clicking an Object
function ClassName::OnMouseUp(%this, %modifier, %worldPosition, %clicks)
{
//code here
}

We will use the above template to code our four buttons. Open your scripting IDE and locate the game.cs file under the gamescript folder. Once opened paste the following code at the bottom of the file.

Game.cs
function Multiply1X::OnMouseUp(%this, %modifier, %worldPosition, %clicks)
{
Hand_Min.setAngularVelocity(180);
Hand_Hour.setAngularVelocity(15);
}

The code above will control our x1 button changing the angular velocity of the minute and hour hands when an object with a class name of Multiply1X is click. If you run your project now the computer will be unable to find the objects Hand_Min and
Hand_Hour because we haven’t named them yet.

Let’s name our hand object. Open TGB and click the minute hand and under the scripting tab set the name field to Hand_Min. Select the fatter hour hand and change its name to Hand_Hour. Save the level and run your project. You should now be about to click the 1x button changing the speed of the hands. Now we just need to program the other three buttons. Add the following code to the bottom of your game.cs.

 

Game.cs

function Multiply2X::OnMouseUp(%this, %modifier, %worldPosition, %clicks)
{
Hand_Min.setAngularVelocity(360);
Hand_Hour.setAngularVelocity(30);
}

function Multiply3X::OnMouseUp(%this, %modifier, %worldPosition, %clicks)
{
Hand_Min.setAngularVelocity(540);
Hand_Hour.setAngularVelocity(45);
}

function Multiply4X::OnMouseUp(%this, %modifier, %worldPosition, %clicks)
{
Hand_Min.setAngularVelocity(720);
Hand_Hour.setAngularVelocity(60);
}

Save the file and run your project. All the buttons should be changing the velocity of the clocks. Congratulations on completing your first project. The next lesson will be the first 20 pages of the Sync Reloaded Project.

 Previous Page

Quick Dev Resources

Absolute Beginner's Guide

Torque Game Builder Tutorials

Graphics Tutorials

Audio Tutorials

 

Powerful TGB Resources

Torque Script Interactive Tutorial

Sync Reloaded and Hands on Coding Guide

Copyrite Making Indie Games 2010