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!
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!