PDA

View Full Version : Horse racing



fried
July 24th, 2007, 05:08 PM
Hi guys,

I would like to make a 100m dash game. the faster the user presses the left and right keys the faster the player moves. Can anyone point in the direction of a tutorial to get me started.

Thanks in advance.

Charleh
July 24th, 2007, 05:23 PM
I don't think you'll find a tutorial like this on the net... I haven't seen any 'track and field' style games tutted. It's a simple premise though - what exactly are you stuck on?

fried
July 24th, 2007, 05:34 PM
Well basically I have to check the speed of which the user presses the keys. The keys will basically be Left and Right. The faster the user presses these keys the faster the horse will move...

The_Doctor
July 24th, 2007, 09:34 PM
You just need to check the time between each time u press the key

its sumthing like this

ifkey.isdown.key.right{
lasttime=gettimer()
}
ifkey.isdown.key.left{
newtime=gettimer()
ifnewtime+100>lasttime{
x++
}
}


Some thing like that

Charleh
July 25th, 2007, 09:56 AM
You just need to check the time between each time u press the key

its sumthing like this

ifkey.isdown.key.right{
lasttime=gettimer()
}
ifkey.isdown.key.left{
newtime=gettimer()
ifnewtime+100>lasttime{
x++
}
}


Some thing like that

I'd approach it with a counter which holds the speed of your horse. This speed decrements towards 0 every frame.

Another boolean value determines what the user has to hit next to get an increase in speed - when the user presses the left key and the boolean is not set the boolean gets set, and vice-versa - each time a successful set/unset is done, add some value to the speed counter

Cap the speed counter to a maximum value and there you go - all set!

for instance

in AlmostPseudoCode (tm)



var Speed:Number;
var Accel:Number;
var LeftKeyHit:Boolean;

Speed = 0;
Accel = 4;

onEnterFrame = function() {
if(Key.isDown(Key.LEFT) && !LeftKeyHit) {
LeftKeyHit = true;
AddSpeed(Accel);
} else if (Key.isDown(Key.RIGHT) && LeftKeyHit) {
LeftKeyHit = false;
AddSpeed(Accel);
}

if(Speed > 0) {
Speed--;
}
}

function AddSpeed(spd:Number) {
Speed += spd;
if(Speed > 100) {
Speed = 100;
}
}


You might need to tweak it - you probably also want to increase speed inversely proportionally - so the faster you go the harder it is to go faster....

You can do this by subtracting the current speed from the maximum speed, and finding out what percentage of the maximum speed is remaining - then you can multiply acceleration by this percentage - so the closer you get to 100% speed the faster you have to hit the buttons to add a bit more speed!