PDA

View Full Version : Motion path, random/ change of speed


dangrey
11-11-2010, 01:29 PM
Hi,

I have a race-track with 4 cars that each follows a motion path. The goal of the game is that a random wins. Is it possible to set random speed for those cars, and that the speed changes several times during the 'race'?
E.g. the cars get new random speed after every corner.

Eralmidia
11-12-2010, 11:13 AM
Just use the
Math.random()
The actual formula depends a bit on what numbers you use for the game. At i simplest form, just using the Math.random() will genereate a number n where 0 <= n < 1, so for instance:


var baseSpeed:int = 5;
function setSpeed():void
{
var speed = baseSpeed + Math.random();
//speed is between 5 and 5.99999
}


But you have a lot of freedom with random combining it with other functionality of the Math class, like the floor method.

Take a look at http://www.kirupa.com/developer/actionscript/tricks/random.htm for a few examples :)

dangrey
11-12-2010, 02:07 PM
Thanks for the information :) I'm now stuck on how draw a motion-path in AC3, make the car follow it and change the speed several times during the race. I can make it work with the tools, but only for a demonstration. And then the speed is constant (with easing).

Eralmidia
11-12-2010, 08:39 PM
Yeah, thats because you're kinda mixing the designers and the programmers world here. The motion path tool is for designers, and if you want more power, you will need to learn some codes to get the job done.

By changing the .x and .y (and, as of late z) properties of a movie clip for instance, you can change it's position. Combining this with some math can create advanced movements, but this depends on you expertise of math I guess.

If you just want the cars to run in the same circle, you can simulate the same tweening you are doing with frames on the timeline, by using a tween library. Flash got one buildt in, http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/transitions/Tween.html, or you can check out another tween library like gTween, Tweener og TweenLite.