PDA

View Full Version : Trying to understand this driving game script!


dmc900
09-02-2008, 07:48 AM
I used to know a lot about action script programming, but I haven’t used it for years and I feel like I’ve forgotten everything. I’ve refreshed my mind on the basics and some of the more difficult things which are annoying me.

I recently have wanted to use flash action script to program a driving game.
I want to get my vehicle moving around; I found a tutorial which is fairly helpful basically giving me the code but not explaining the code and how it works very well.

I’m following the tutorial/code provided here
http://www.emanueleferonato.com/2007/05/15/create-a-flash-racing-game-tutorial/

I’m only trying to understand the first part, “Moving the car”, for the rest I’m using my own methods which are much simpler to understand.

Below is the code, I removed any unnecessary stuff.

acceleration = 0.4;
speedDecay = 0.96;
rotationStep = 10;
maxSpeed = 10;
backSpeed = 1;

function step(who) {
//we will constantly decrease speed by multiplying it with a number below 1
if (this["speed"+who]>0.3) {
this["speed"+who] *= _root.speedDecay;
} else {
this["speed"+who] = 0;
}
//the car will react to certain keys
//accelerate
if (Key.isDown(Key.UP) && this["speed"+who]<_root.maxSpeed) {
this["speed"+who] += _root.acceleration;
}
//brake (reverse)
if (Key.isDown(Key.DOWN)) {
this["speed"+who] -= _root.backSpeed;
}
//steer left
if (Key.isDown(Key.LEFT) && this["speed"+who]>0.3) {
_root["car"+who]._rotation -= _root.rotationStep*(this["speed"+who]/_root.maxSpeed);
}
//steer right
if (Key.isDown(Key.RIGHT) && this["speed"+who]>0.3) {
_root["car"+who]._rotation += _root.rotationStep*(this["speed"+who]/_root.maxSpeed);
}
this["rotation"+who] = _root["car"+who]._rotation;
//we calculate the two components of speed (X axis and Y axis)
this["speedx"+who] = Math.sin(this["rotation"+who]*(Math.PI/180))*this["speed"+who];
this["speedy"+who] = Math.cos(this["rotation"+who]*(Math.PI/180))*this["speed"+who]*-1;
//apply the components on the actual position of the car
_root["car"+who]._x += this["speedx"+who];
_root["car"+who]._y += this["speedy"+who];
}

I’d appreciate it if someone could go through each line and talk about how everything works.

for example

line 1
function step(who) {
The function step has been defined. “Who” in Brackets means?


I understand the second someone sees this topic, expecting them to do so much work. I understand, if only you’re nice enough, and you’ll get a big thankyou.

But knowing that everyone probably doesn’t have much time to spend walking me through this here are some quick questions I have.


line 1
function step(who) {

I’m not exactly sure what the who achieves there and how it interacts with lines 3, 4, 6, ect... .?

3 if (this["speed"+who]>0.3) {
4 this["speed"+who] *= _root.speedDecay;
5 } else {
6 this["speed"+who] = 0;
7 }



Thanks for your time.

yogeshpuri
09-02-2008, 08:27 AM
Hi

The who is instance number of car you want to move i.e. if you have more than one cars then you can give instance name like car1, car2 etc. This gets called from the blank movieclip on left top corner (onEnterFrame)

Similarly a speed variable named speed1 = 0 is declared there on that movieClip for initial speed. Hope this will give you a way.