Game Development Community

Clock - almost working but a little off

by amaranthia · in Torque Game Builder · 03/28/2008 (6:26 pm) · 5 replies

I'm creating a behavior that lets you have an old-fashioned clock in your game. When you start a level, the clock hand starts at 12 and moves clockwise. When the level ends, the clock hand should stop at 12.

My script is almost working, but I have one little problem... If I assign a level to a large number of minutes (5), the clock hand usually stops at 11. If I assign a level to a small number of minutes (0.5), the clock hand goes past 12 and stops at 1.

I'm missing a tiny piece, but I'm not sure where it is. Does anyone see the missing link?

2zs8ja.blu.livefilestore.com/y1psrD4zltB8WG59KrLSZerCkkzandzGpUBDMau2V0oKKpC6VkamBAY7c1UooBO7OA9sGUnGpCNgnoGAipROxl8OA/clock.JPG
if (!isObject(ClockBehavior))
{
   %template = new BehaviorTemplate(ClockBehavior);
   
   %template.friendlyName = "Clock";
   %template.behaviorType = "GUI";
   %template.description  = "Move the hand of a clock 360 degrees and then stop";

   %template.addBehaviorField(minutes, "minutes to get around the clock", float, "0.03");
}

function ClockBehavior::onBehaviorAdd(%this)
{
   %this.time = (%this.minutes * 60000); 
   %this.speed = %this.time / (10000 * %this.minutes * %this.minutes);
   %this.schedule(100, "start");
}

function ClockBehavior::start(%this)
{
   //move arrow
   %this.schedule(%this.time, "stop");
   %this.owner.setAngularVelocity(%this.speed);
}

function ClockBehavior::stop(%this)
{
   %this.owner.setAngularVelocity(0);
}

#1
03/30/2008 (7:03 pm)
As far as I can see the code looks right. Are the timings off ? For example when you assign it a large amount of minutes does and it stops at 11 should it go longer ? or did it stop at the right time and it happened to be at 11 ? I would modify the behavior and set an autorotation target:

function ClockBehavior::onBehaviorAdd(%this)
{
   %this.time = (%this.minutes * 60000);
   %this.speed = %this.time / (10000 * %this.minutes * %this.minutes);
   $timing = %this.schedule(100, "start");
}

function ClockBehavior::start(%this)
{
    //move arrow
   %this.owner.setAngularVelocity(%this.speed);
   %this.owner.setRotationTarget(0,true,true,true,0.001) 
}

function ClockBehavior::onRotationTarget(%this)
{
   echo (getTimeSinceStart($timing))
}

I think that code should be OK. If your timings are good you should just use a rotation target and use the callback function to end your level.

See how you go.
#2
03/30/2008 (7:56 pm)
Okay, I made a small change and everything worked right. Thanks for the tip!

if (!isObject(ClockBehavior))
{
   %template = new BehaviorTemplate(ClockBehavior);
   
   %template.friendlyName = "Clock";
   %template.behaviorType = "GUI";
   %template.description  = "Move the hand of a clock 360 degrees and then stop";

   %template.addBehaviorField(minutes, "minutes to get around the clock", float, ".25");
}

function ClockBehavior::onBehaviorAdd(%this)
{
   %this.time = (%this.minutes * 60000); 
   %this.speed = %this.time / (10000 * (%this.minutes * %this.minutes));
   %this.schedule(100, "start");
}

function ClockBehavior::start(%this)
{
   //move arrow
   %this.owner.setAngularVelocity(%this.speed);
   %this.owner.setRotationTarget(180, true, true, true, 0.1);  
}

function ClockBehavior::onRotationTarget(%this)
{ 
   %this.owner.setAngularVelocity(%this.speed);
   %this.owner.setRotationTarget(360, true, true, true, 0.1);  
}

function ClockBehavior::stop(%this)
{
   %this.owner.setAngularVelocity(0);
   scene::endDay();
   
}
#3
04/04/2008 (3:01 pm)
Glad to see it's working now. BTW, love the clock art!
#4
04/04/2008 (9:45 pm)
Thanks! I'll tell my artist. This game is going to be cute enough to give you a toothache. Barbie, eat your heart out! *cackles*
#5
04/11/2008 (2:16 pm)
I have to admit, that is a beautiful silly clock thing