PDA

View Full Version : [AS3] Random horse races


PrinceOfPersia
09-09-2009, 12:51 AM
Hi, well i have a little game of horse races. each horse should move on the x axis at random speed. i tried just making a random number variable and move the movie clip acording to the generated value, but the horse would have only one constant speed, so the winning horse would be defined from the beggining. So i put a timer, so the speed change constantly making it harder to guess which horse is going to win. the problem is that all the horses move too close, i mean, their speed are diferent, but to close, i need a more variable speed.
this is my code.


stop();

// this is the timer for changing the speed of the horses
var myTimer:Timer = new Timer(6,1000);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerStart);

//this is for giving the horses a random speed and move x position based on the result
function timerStart(TimerEvent):void
{
var speed1:Number = Math.random();
caballo1.x += speed1;

var speed2:Number = Math.random();
caballo2.x += speed2;

var speed3:Number = Math.random();
caballo3.x += speed3;

}

// This is the restart button function and elistener.
reStart.addEventListener(MouseEvent.CLICK, restartRace);

function restartRace(MouseEvent):void
{
myTimer.stop();
caballo1.x = 0
caballo2.x = 0
caballo3.x = 0
gotoAndStop(1);
}


If i make it without the timer event, the horses never stop and they go on and on, how can i make they stop when they reach the edge of screen?

Thanks!

lordofduct
09-09-2009, 06:56 AM
Math.random() generates a number between 0->0.9999

multiply it by some scalar to get more robust values.

Tilpo
09-09-2009, 07:20 AM
function timerStart(TimerEvent):void
{
if(caballo1.x<stage.stageWidth&&caballo2.x<stage.stageWidth&&caballo3.x<stage.stageWidth) {
var speed1:Number = Math.random();
caballo1.x += speed1;

var speed2:Number = Math.random();
caballo2.x += speed2;

var speed3:Number = Math.random();
caballo3.x += speed3;
}

}

henke37
09-09-2009, 01:23 PM
Adding random values over time changes the number distrubution to favour the averenge instead of an even distrububtion.

lordofduct
09-09-2009, 01:46 PM
Adding random values over time changes the number distrubution to favour the averenge instead of an even distrububtion.

Wouldn't average be even distribution?


But yeah, henke37 has a point. Math.random() as a random number generator has an equal probability over time. Basically meaning all values between 0 ->0.9999 have an equal chance of being grabbed.

Being so the limit of f(x) = random(0->1) approaching infinity is 0.5

hence why it's not a real random number generator and instead a psuedo-random number generator.

PrinceOfPersia
09-19-2009, 09:26 PM
i actualy did this, put an if statemet for the horses to reach the end of screen, but it didnt work, they continue moving till the timer is over. =(

neilmmm
09-20-2009, 10:07 PM
not sure if this helps but this is how i would do it in as2

var pos1:Number = _1_mc._x;
var finishPos:Number = line_mc._x;
var speed1:Number;
var speed2:Number;
var speed3:Number;
var speed4:Number;
start_mc.onRelease = function() {
reset_mc.enabled = false;
this.enabled = false;
this.onEnterFrame = function() {
for (m=1; m<5; m++) {
this._parent["speed"+m] = Math.ceil(Math.random()*5);
}
for (k=1; k<5; k++) {
this._parent["_"+k+"_mc"]._x += this._parent["speed"+k];
}
for (i=1; i<5; i++) {
if (this._parent["_"+i+"_mc"]._x>finishPos) {
delete this.onEnterFrame;
my_txt.text = "Horse "+i+" won!";
reset_mc.enabled = true;
}
}
};
};
reset_mc.onRelease = function() {
my_txt.text = "";
start_mc.enabled = true;
for (i=1; i<5; i++) {
this._parent["_"+i+"_mc"]._x = pos1;
}
};

bebo1488
09-21-2009, 11:08 AM
Thanks it worked

neilmmm
09-21-2009, 05:22 PM
yw