View Full Version : [AS2] creating a random race game

01-25-2007, 08:22 PM
Hi all,

I'm new to the forum and am so glad that I found it. I wanted to see if anyone knew of a tutorial to make a race game that would randomly generate outcomes. For example, I thought about a horse race that simply has a play button. Once you hit play, each time it will randomly let one of them win. At the conclusion, there is a replay option. Any ideas? Thanks in advance!


01-25-2007, 08:29 PM
Just use the "Math.random()" function. Have a bunch of outcomes listed, like:


Then just use the Math.random() function to randomly call one.

01-25-2007, 09:44 PM
Well now i'm trying to learn how to use the the arrays and related about them so i think this is a good excersice.
Also if somebody see or now anything i miss please tell me... for example i think this line can be different and no so complicated:
positions.push(myHorses[j].split(" "));
and for the next example just copy and paste the code in first frame... about the buttom i think u can do it :)

myHorses = new Array();
positions = new Array();
for (i=1; i<6; i++) {
this.createTextField("horse"+i, this.getNextHighestDepth(), 20, 25*i, 250, 20);
this["horse"+i].stats = Math.random();
myHorses.push(this["horse"+i].stats+" "+this["horse"+i]._name);
for (j=0; j<5; j++) {
positions.push(myHorses[j].split(" "));
this["horse"+(1+j)].text = "position #"+(1+j)+": "+positions[j][1];

And please feed back =D

01-27-2007, 01:34 AM
I've been able to figure out how to do that with just having the objects move to right from left using math.random (I think...see below). I'm wondering, though, how do I get the racing movie clips to follow a motion trail instead of just moving from left to right. Any ideas? Thanks!

// ON movieclip
onClipEvent (enterFrame) {
// Start IF statement
if (_root.raceover == 0) {
// ID of DOT ( change this on every dot 1-2-3 etx)
id = 1;
// Movement of DOT
ran = math.round(math.random()*7+3);
this._x += ran;
// Finnish line
if (this._x>500) {
_root.raceover = 1;
_root.winner = id;
// Get all dots to return
if (_root.raceover == 1) {
// Start porition
this._x = 25;

01-27-2007, 07:14 AM
well now that have done this i know where i will use this on my portfolio (that i will finish in some weeks :D)
all of you can make your beats... copy and paste the code... i think horse 3 is going to win!!!

myHorses = new Array();
positions = new Array();
for (i=1; i<6; i++) {
this.createTextField("horse"+i, this.getNextHighestDepth(), 20, 25*i, 250, 20);
this["horse"+i].stats = Math.random()+0.5;
myHorses.push(this["horse"+i].stats+" "+this["horse"+i]._name);
for (j=0; j<5; j++) {
positions.push(myHorses[j].split(" "));
this["horse"+(1+j)].text = "position #"+(1+j)+": ";
bola = this["circle"+(j+1)];
bola.num = j;
k = 0;
bola.onEnterFrame = function() {
this._x += 10*positions[this.num][0];
if (this._x>=300) {
k += 1;
this._x = 300;
_root["horse"+(k)].text = "position #"+(k)+": "+positions[this.num][1];
delete this.onEnterFrame;
function circle() {
this.createTextField("horse_circle"+i, this.getNextHighestDepth(), 20, i, 250, 20);
this.createEmptyMovieClip("circle"+i, this.getNextHighestDepth());
r = 250;
g = 50*i;
b = 50*i;
rgb = "0x"+r.toString(16)+g.toString(16)+b.toString(16);
this["circle"+i].lineStyle(0, 0x000000);
this["horse_circle"+i].text = "horse"+i;
this["horse_circle"+i]._y = 190+(20*i);
this["circle"+i]._y = 200;
drawCircle(this["circle"+i], 80, 20*i, 5);
function drawCircle(mc:MovieClip, x:Number, y:Number, r:Number):Void {
mc.moveTo(x+r, y);
mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
mc.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);

hope somebody finds useful this info XD
and nop... i don't know how to fallow the pah senocular has something related

01-27-2007, 09:43 AM
that is impresive

I am trying to do something simular but with an AS file seperate

any ideas on how to to fix this problem....

I can't seem to get the "finish" to stop the race, I have to put in a number manually :confused:


public static var finish:Number;
public function setFinish(f:Number):Void {
finish = f;
public function run():Void {
if (horse_mc._x<=finish) {
horse_mc._x = horse_mc._x+speed;
} else {