# Problem with racing game

**URL:** <https://forum.kirupa.com/t/problem-with-racing-game/118641>\
**Category:** Uncategorized\
**Created:** [August 7, 2004, 5:57pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641 "2004-08-07T17:57:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilayeggs](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@ilayeggs](https://forum.kirupa.com/u/ilayeggs)\
**Post date:** [August 7, 2004, 5:57pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641/1 "2004-08-07T17:57:08Z")

</div>

hi everyone,  
i’m currently working on a racing game, and i’m basing my code on a tutorial i found in a magazine. i’ve copied the tutorial word for word (except for a few values i changed) but i can’t seem to get the car to drive!! i’m 99% sure the problem is in the car code because everything else seems to work. here’s the part of the code where i think the problem is:

function initCar() {  
car.\_x = 300;  
car.\_y = 75;  
car.\_rotation = 0;  
car.velX = 0;  
car.velY = 0;  
car.velR = 0;  
car.accel = 0;  
car.steering = 0;  
car.points = 0;  
car.onEnterFrame = drive;  
}  
function drive() {  
this.leftWheel.\_rotation = this.steering;  
this.rightWheel.\_rotation = this.steering;  
var speed = Math.sqrt(this.velX \* this.velX + this.velY \* this.velY);  
this.velR += this.steering \* speed / 500;  
if (this.accel \< 0) {  
this.\_rotation -= this.velR;  
} else {  
this.\_rotation += this.velR;  
}  
var rad = this.\_rotation \* Math.PI / 180;  
this.velX += Math.cos(rad) \* this.accel;  
this.velY += Math.sin(rad) \* this.accel;  
this.\_x += this.velX;  
this.\_y += this.velY;  
if (!track.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
this.velX \*= .6;  
this.velY \*= .6;  
} else {  
this.velX \*= .9;  
this.velY \*= .9;  
}  
this.velR \*= .9;  
}  
onEnterFrame = function () {  
car.steering = 0;  
car.accel = 0;  
if (Key.isDown(Key.LEFT)) {  
car.steering = -40;  
} else if (Key.isDown(Key.RIGHT)) {  
car.steering = 40;  
}  
if (Key.isDown(Key.UP)) {  
car.accel = 1;  
} else if (Key.isDown(Key.DOWN)) {  
car.accel = -.5;  
}  
};

the tutorial says it’s written for flash mx (which is what i’m using), but some of the other tutorials in the magazine are for mx 2004. i’m pretty sure this one should work but it’s not.

if you have any questions about the code please ask. please reply if you know what the problem is.

---

<div class="post-metadata">

**Author:** ![stringy](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@stringy](https://forum.kirupa.com/u/stringy)\
**Post date:** [August 7, 2004, 6:29pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641/2 "2004-08-07T18:29:59Z")

</div>

> [@ilayeggs](#):
>
> hi everyone,  
> i’m currently working on a racing game, and i’m basing my code on a tutorial i found in a magazine. i’ve copied the tutorial word for word (except for a few values i changed) but i can’t seem to get the car to drive!! i’m 99% sure the problem is in the car code because everything else seems to work. here’s the part of the code where i think the problem is:
> 
> function initCar() {  
> car.\_x = 300;  
> car.\_y = 75;  
> car.\_rotation = 0;  
> car.velX = 0;  
> car.velY = 0;  
> car.velR = 0;  
> car.accel = 0;  
> car.steering = 0;  
> car.points = 0;  
> car.onEnterFrame = drive;  
> }  
> function drive() {  
> this.leftWheel.\_rotation = this.steering;  
> this.rightWheel.\_rotation = this.steering;  
> var speed = Math.sqrt(this.velX \* this.velX + this.velY \* this.velY);  
> this.velR += this.steering \* speed / 500;  
> if (this.accel \< 0) {  
> this.\_rotation -= this.velR;  
> } else {  
> this.\_rotation += this.velR;  
> }  
> var rad = this.\_rotation \* Math.PI / 180;  
> this.velX += Math.cos(rad) \* this.accel;  
> this.velY += Math.sin(rad) \* this.accel;  
> this.\_x += this.velX;  
> this.\_y += this.velY;  
> if (!track.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
> this.velX \*= .6;  
> this.velY \*= .6;  
> } else {  
> this.velX \*= .9;  
> this.velY \*= .9;  
> }  
> this.velR \*= .9;  
> }  
> onEnterFrame = function () {  
> car.steering = 0;  
> car.accel = 0;  
> if (Key.isDown(Key.LEFT)) {  
> car.steering = -40;  
> } else if (Key.isDown(Key.RIGHT)) {  
> car.steering = 40;  
> }  
> if (Key.isDown(Key.UP)) {  
> car.accel = 1;  
> } else if (Key.isDown(Key.DOWN)) {  
> car.accel = -.5;  
> }  
> };
> 
> the tutorial says it’s written for flash mx (which is what i’m using), but some of the other tutorials in the magazine are for mx 2004. i’m pretty sure this one should work but it’s not.
> 
> if you have any questions about the code please ask. please reply if you know what the problem is.

you have not called the function initcar,just type initcar() after the rest of the code

---

<div class="post-metadata">

**Author:** ![ilayeggs](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@ilayeggs](https://forum.kirupa.com/u/ilayeggs)\
**Post date:** [August 7, 2004, 7:28pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641/3 "2004-08-07T19:28:17Z")

</div>

no, i called initCar() in another part of the code where i know the problem isn’t

---

<div class="post-metadata">

**Author:** ![ilayeggs](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@ilayeggs](https://forum.kirupa.com/u/ilayeggs)\
**Post date:** [August 7, 2004, 7:33pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641/4 "2004-08-07T19:33:17Z")

</div>

in case it helps, here’s the entire code:

initRace(“Press any key to start”);  
function initTrack() {  
createEmptyMovieClip(“race”, 0);  
track = race.attachMovie(“track”, “track”, 0);  
car = race.attachMovie(“car”, “car”, 1);  
}  
function initCar() {  
car.\_x = 300;  
car.\_y = 75;  
car.\_rotation = 0;  
car.velX = 0;  
car.velY = 0;  
car.velR = 0;  
car.accel = 0;  
car.steering = 0;  
car.points = 0;  
car.onEnterFrame = drive;  
}  
function drive() {  
this.leftWheel.\_rotation = this.steering;  
this.rightWheel.\_rotation = this.steering;  
var speed = Math.sqrt(this.velX \* this.velX + this.velY \* this.velY);  
this.velR += this.steering \* speed / 500;  
if (this.accel \< 0) {  
this.\_rotation -= this.velR;  
} else {  
this.\_rotation += this.velR;  
}  
var rad = this.\_rotation \* Math.PI / 180;  
this.velX += Math.cos(rad) \* this.accel;  
this.velY += Math.sin(rad) \* this.accel;  
this.\_x += this.velX;  
this.\_y += this.velY;  
if (!track.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
this.velX \*= .6;  
this.velY \*= .6;  
} else {  
this.velX \*= .9;  
this.velY \*= .9;  
}  
this.velR \*= .9;  
if (track.finish.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
if (!done) {  
clearInterval(timeInt);  
showBanner(“Finished!”);  
done = true;  
}  
}  
for (var i = 0; i \< numCheckPoints; i++) {  
var checkPoint = track[“checkPoint” + i];  
if (checkPoint.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
this.points += checkPoint.value;  
checkPoint.value = 0;  
showBanner("Checkpoint " + this.points);  
}  
}  
if ((track.finish.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) && (this.points \>= numCheckPoints)) {  
if (!done) {  
clearInterval(timeInt);  
showBanner(“Finished!”);  
done = true;  
endGame();  
}  
}  
}  
onEnterFrame = function () {  
car.steering = 0;  
car.accel = 0;  
if (Key.isDown(Key.LEFT)) {  
car.steering = -40;  
} else if (Key.isDown(Key.RIGHT)) {  
car.steering = 40;  
}  
if (Key.isDown(Key.UP)) {  
car.accel = 1;  
} else if (Key.isDown(Key.DOWN)) {  
car.accel = -.5;  
}  
targX = Stage.width / 2 - car.\_x;  
targY = Stage.height / 2 - car.\_y;  
race.\_x += (targX - race.\_x) / 10;  
race.\_y += (targY - race.\_y) / 10;  
};  
function initRace(message) {  
attachMovie(“display”, “display”, 1);  
done = false;  
Key.addListener(this);  
onKeyDown = startGame;  
showBanner(message);  
initCar();  
initTrack();  
}  
function startGame() {  
elapsed = 0;  
timeFunc();  
timeInt = setInterval(timeFunc, 100);  
delete onKeyDown;  
}  
function timeFunc() {  
var mins = Math.floor(elapsed / 600);  
if (mins \< 10) {  
mins = “0” + mins;  
}  
var secs = Math.floor((elapsed % 600) / 10);  
if (secs \< 10) {  
secs = “0” + secs;  
}  
var tens = elapsed % 10;  
display.timer.text = mins + “:” + secs + “.” + tens;  
elapsed++;  
}  
function showBanner(msg) {  
attachMovie(“banner”, “banner”, 2);  
banner.\_x = Stage.width / 2;  
banner.\_y = Stage.height / 2;  
banner.\_xscale = banner.\_yscale = 75;  
banner.label.text = msg;  
banner.onEnterFrame = grow;  
}  
function grow() {  
this.\_alpha -= 1;  
this.\_xscale = this.\_yscale += 5;  
if (this.\_xscale \> 450) {  
this.removeMovieClip();  
}  
}  
function initCheckPoints() {  
numCheckPoints = 2;  
for (var i = 0; i \< numCheckPoints; i++) {  
var checkPoint = track[“checkPoint” + i];  
checkPoint.value = 1;  
}  
}  
function endGame() {  
restartInt = setInterval(restart, 5000);  
}  
function restart() {  
clearInterval(restartInt);  
initCheckPoints();  
initCar();  
initRace(“Play again”);  
}

---

<div class="post-metadata">

**Author:** ![stringy](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@stringy](https://forum.kirupa.com/u/stringy)\
**Post date:** [August 7, 2004, 7:49pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641/5 "2004-08-07T19:49:24Z")

</div>

> [@ilayeggs](#):
>
> in case it helps, here’s the entire code:
> 
> initRace(“Press any key to start”);  
> function initTrack() {  
> createEmptyMovieClip(“race”, 0);  
> track = race.attachMovie(“track”, “track”, 0);  
> car = race.attachMovie(“car”, “car”, 1);  
> }  
> function initCar() {  
> car.\_x = 300;  
> car.\_y = 75;  
> car.\_rotation = 0;  
> car.velX = 0;  
> car.velY = 0;  
> car.velR = 0;  
> car.accel = 0;  
> car.steering = 0;  
> car.points = 0;  
> car.onEnterFrame = drive;  
> }  
> function drive() {  
> this.leftWheel.\_rotation = this.steering;  
> this.rightWheel.\_rotation = this.steering;  
> var speed = Math.sqrt(this.velX \* this.velX + this.velY \* this.velY);  
> this.velR += this.steering \* speed / 500;  
> if (this.accel \< 0) {  
> this.\_rotation -= this.velR;  
> } else {  
> this.\_rotation += this.velR;  
> }  
> var rad = this.\_rotation \* Math.PI / 180;  
> this.velX += Math.cos(rad) \* this.accel;  
> this.velY += Math.sin(rad) \* this.accel;  
> this.\_x += this.velX;  
> this.\_y += this.velY;  
> if (!track.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
> this.velX \*= .6;  
> this.velY \*= .6;  
> } else {  
> this.velX \*= .9;  
> this.velY \*= .9;  
> }  
> this.velR \*= .9;  
> if (track.finish.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
> if (!done) {  
> clearInterval(timeInt);  
> showBanner(“Finished!”);  
> done = true;  
> }  
> }  
> for (var i = 0; i \< numCheckPoints; i++) {  
> var checkPoint = track[“checkPoint” + i];  
> if (checkPoint.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) {  
> this.points += checkPoint.value;  
> checkPoint.value = 0;  
> showBanner("Checkpoint " + this.points);  
> }  
> }  
> if ((track.finish.hitTest(this.\_x + race.\_x, this.\_y + race.\_y, true)) && (this.points \>= numCheckPoints)) {  
> if (!done) {  
> clearInterval(timeInt);  
> showBanner(“Finished!”);  
> done = true;  
> endGame();  
> }  
> }  
> }  
> onEnterFrame = function () {  
> car.steering = 0;  
> car.accel = 0;  
> if (Key.isDown(Key.LEFT)) {  
> car.steering = -40;  
> } else if (Key.isDown(Key.RIGHT)) {  
> car.steering = 40;  
> }  
> if (Key.isDown(Key.UP)) {  
> car.accel = 1;  
> } else if (Key.isDown(Key.DOWN)) {  
> car.accel = -.5;  
> }  
> targX = Stage.width / 2 - car.\_x;  
> targY = Stage.height / 2 - car.\_y;  
> race.\_x += (targX - race.\_x) / 10;  
> race.\_y += (targY - race.\_y) / 10;  
> };  
> function initRace(message) {  
> attachMovie(“display”, “display”, 1);  
> done = false;  
> Key.addListener(this);  
> onKeyDown = startGame;  
> showBanner(message);  
> initCar();  
> initTrack();  
> }  
> function startGame() {  
> elapsed = 0;  
> timeFunc();  
> timeInt = setInterval(timeFunc, 100);  
> delete onKeyDown;  
> }  
> function timeFunc() {  
> var mins = Math.floor(elapsed / 600);  
> if (mins \< 10) {  
> mins = “0” + mins;  
> }  
> var secs = Math.floor((elapsed % 600) / 10);  
> if (secs \< 10) {  
> secs = “0” + secs;  
> }  
> var tens = elapsed % 10;  
> display.timer.text = mins + “:” + secs + “.” + tens;  
> elapsed++;  
> }  
> function showBanner(msg) {  
> attachMovie(“banner”, “banner”, 2);  
> banner.\_x = Stage.width / 2;  
> banner.\_y = Stage.height / 2;  
> banner.\_xscale = banner.\_yscale = 75;  
> banner.label.text = msg;  
> banner.onEnterFrame = grow;  
> }  
> function grow() {  
> this.\_alpha -= 1;  
> this.\_xscale = this.\_yscale += 5;  
> if (this.\_xscale \> 450) {  
> this.removeMovieClip();  
> }  
> }  
> function initCheckPoints() {  
> numCheckPoints = 2;  
> for (var i = 0; i \< numCheckPoints; i++) {  
> var checkPoint = track[“checkPoint” + i];  
> checkPoint.value = 1;  
> }  
> }  
> function endGame() {  
> restartInt = setInterval(restart, 5000);  
> }  
> function restart() {  
> clearInterval(restartInt);  
> initCheckPoints();  
> initCar();  
> initRace(“Play again”);  
> }

try changing the order of this function to this  
function initRace(message) {  
attachMovie(“display”, “display”, 1);  
done = false;  
Key.addListener(this);  
onKeyDown = startGame;  
showBanner(message);  
initTrack();  
initCar();

}  
If this is not the problem, you may be better posting the fla so someone can have a look.

---

<div class="post-metadata">

**Author:** ![ilayeggs](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@ilayeggs](https://forum.kirupa.com/u/ilayeggs)\
**Post date:** [August 7, 2004, 8:04pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641/6 "2004-08-07T20:04:25Z")

</div>

holy crap! i didn’t think that would work but i tried it and it did; now all i need 2 do is reposition the car. thx a lot!! 😃

---

<div class="post-metadata">

**Author:** ![stringy](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@stringy](https://forum.kirupa.com/u/stringy)\
**Post date:** [August 7, 2004, 8:08pm UTC](https://forum.kirupa.com/t/problem-with-racing-game/118641/7 "2004-08-07T20:08:24Z")

</div>

> [@ilayeggs](#):
>
> holy crap! i didn’t think that would work but i tried it and it did; now all i need 2 do is reposition the car. thx a lot!! 😃

you are welcome
