all groups > flash actionscript > august 2005 >
You're in the

flash actionscript

group:

got the car game turning, now another hickup


got the car game turning, now another hickup Skee-Lo
8/31/2005 11:25:44 PM
flash actionscript:
hi all........... again.

all your help with getting the car to turn was a very big help and now woth
some minor tweeks it is turning and startin to look real good (hooorayyyyyy)
but now the prob is getting the car to drive in the direction that the player
has it pionted in, which would be kinda handy. with this i want to still keep
the acceleration of the car. also, (yes theres more) if i want the car to slow
down when i take my finger off the go button, the up key.

the code i have is as follows...............


onClipEvent (enterFrame) {
this._x = this._x+_root.xspeed;
this._y = this._y+_root.yspeed;
if (Key.isDown(Key.RIGHT)) {
//turn right
this._rotation = this._rotation+turnspeed;
}
if (Key.isDown(Key.LEFT)) {
//turn left
this._rotation = this._rotation-turnspeed;
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.UP)) {
_root.yspeed = _root.yspeed-1;
}
if (Key.isDown(Key.DOWN)) {
_root.yspeed = _root.yspeed+1;
}
}
onClipEvent (load) {
turnspeed = 5;
xspeed = 0;
yspeed = 0;
acceleration = 1;
this.rotation = -90;
PI = Math.PI;
}
this code is placed in the movieclip of my car (the ultimate Lancia Delta Evo
2)

there prob is some code in there that i don't need so if that is the case jus
ignore it or tell us that we don't need it cos i'm not worried at the moment if
i don't need it cos IT'S WORKING AND I'M HAPPY!!!!!!

yes well. this be a great help if you could help us yeah!!!

it could also make us stop talking like a pirate........

thanx people
Re: got the car game turning, now another hickup mandingo
8/31/2005 11:40:18 PM
I once created a tank game... and it turned and drove forward and in reverse

here is how I handled the motion:
// code inside the onEnterFrame event
if(Key.isDown(thisUpKey)){
this.movingForward = true;
this._x += opposite * Math.cos(_rotation*(Math.PI/180))*2;
this._y += opposite * Math.sin(_rotation*(Math.PI/180))*2;
}else{
this.movingForward = false;
}
if(Key.isDown(thisDownKey)){
this.movingBackWard = true;
this._x -= opposite * Math.cos(_rotation*(Math.PI/180))*2;
this._y -= opposite * Math.sin(_rotation*(Math.PI/180))*2;
}else{
this.movingBackWard = false;
}
if(Key.isDown(thisLeftKey)){
this.turningLeft = true;
this._rotation -=3;
}else{
this.turningLeft = false;
}
if(Key.isDown(thisRightKey)){
this.turningRight = true;
this._rotation +=3;
}else{
this.turningRight = false;
}

I hope that helps.

cheers
Re: got the car game turning, now another hickup Skee-Lo
9/7/2005 12:00:00 AM
hey dude.

put that code in and after some time spent it had no errors. but it still
isn't doing what i want it to do.....
as i said i want it to go in the direction that the car is facing. also i am
using Flash MX if that helps. im not very good at code and only should be doing
basic stuff so please give us a hand.

jus in case i made a mistace putting it into the actionscript. this is what i
done....

onClipEvent (enterFrame) {
this._x = this._x+_root.xspeed;
this._y = this._y+_root.yspeed;
if (Key.isDown(Key.RIGHT)) {
//turn right
this._rotation = this._rotation+turnspeed;
}
if (Key.isDown(Key.LEFT)) {
//turn left
this._rotation = this._rotation-turnspeed;
}
if (Key.isDown(thisUpKey)) {
this.movingForward = true;
this._x += opposite*Math.cos(_rotation*(Math.PI/180))*2;
this._y += opposite*Math.sin(_rotation*(Math.PI/180))*2;
} else {
this.movingForward = false;
}
if (Key.isDown(thisDownKey)) {
this.movingBackWard = true;
this._x -= opposite*Math.cos(_rotation*(Math.PI/180))*2;
this._y -= opposite*Math.sin(_rotation*(Math.PI/180))*2;
} else {
this.movingBackWard = false;
}
if (Key.isDown(thisLeftKey)) {
this.turningLeft = true;
this._rotation -= 5;
} else {
this.turningLeft = false;
}
if (Key.isDown(thisRightKey)) {
this.turningRight = true;
this._rotation += 5;
} else {
this.turningRight = false;
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.UP)) {
_root.yspeed = _root.yspeed-1;
}
if (Key.isDown(Key.DOWN)) {
_root.yspeed = _root.yspeed+1;
}
}
onClipEvent (load) {
turnspeed = 5;
xspeed = 0;
yspeed = 0;
acceleration = 1;
this.rotation = 0;
PI = Math.PI;
}

thanx Skee-Lo
Re: got the car game turning, now another hickup NSurveyor
9/7/2005 12:00:00 AM
I'll explain how the movement code works (afaik):

_x += Math.cos(_rotation/180*Math.PI)*speed;
_y += Math.sin(_rotation/180*Math.PI)*speed;

First of all, dividing the number of degrees of rotation by 180*PI will get
the degrees in radians. (ranging from 0pi to 2pi). Say the degrees in radians
is stored in a variable called, rad. If you move an object Math.cos(rad) right
and Math.sin(rad) down, you will move the object 1 pixel from its original spot
in the direction of the degrees you put in. So, if we multiply the xfactor and
yfactor by your speed, you will in the end, move [speed] pixels from the
original spot. Sorry if I butchered anything... haven't taken Trig yet.
Re: got the car game turning, now another hickup NSurveyor
9/7/2005 12:00:00 AM
Added the rotation code, and fixed up your code a bit:

onClipEvent (enterFrame) {
//Slow down slowly
if(speed<0){
speed+=acceleration;
}else if(speed>0){
speed-=acceleration;
}
//Restrict to speed limit
if(speed<-maxspeed){
speed = -maxspeed;
}else if(speed>maxspeed){
speed = maxspeed;
}
//Update position based on rotation and speed
_x += Math.cos(_rotation/180*Math.PI)*speed;
_y += Math.sin(_rotation/180*Math.PI)*speed;
}
onClipEvent (keyDown) {
if (Key.isDown(Key.UP)) {
//speed up to go forwards
speed+=acceleration;
}
if (Key.isDown(Key.DOWN)) {
//speed up to go backwards
speed-=acceleration;
}
if (Key.isDown(Key.RIGHT)) {
//turn right
this._rotation += turnspeed;
}
if (Key.isDown(Key.LEFT)) {
//turn left
this._rotation -= turnspeed;
}
}
onClipEvent (load) {
maxspeed = 20;
turnspeed = 5;
speed = 0;
acceleration = 1;
_rotation = -90;
}

Re: got the car game turning, now another hickup Skee-Lo
9/9/2005 10:00:01 AM
yeh mate.

that still isn't making it go in the way it's facing. is there anyway for us
to make it realise/recognise that the front of the car is the direction that
the foward button should be. or is it worth my while puting in another set of
buttons in to make it drive in the other directions, i don't want to do this
cos it will still be dodgy and yeah we don't want that. sorry i'm writing this
at home and i don't have the code i have but it's pritty much a slightly
modifyed version of the code given above. when i get to school i'll place the
code on heer as well. i'm sudenly realised that this is not as easy as i
thought it would be...................
Re: got the car game turning, now another hickup NSurveyor
9/9/2005 11:24:30 AM
Re: got the car game turning, now another hickup Skee-Lo
9/12/2005 11:08:15 PM
i don't think thats the prob mate.

i gotthe code in as follows......

onClipEvent (enterFrame) {
this._x = this._x+_root.xspeed;
this._y = this._y+_root.yspeed;
if (Key.isDown(Key.RIGHT)) {
//turn right
this._rotation = this._rotation+turnspeed;
}
if (Key.isDown(Key.LEFT)) {
//turn left
this._rotation = this._rotation-turnspeed;
}
if (Key.isDown(thisUpKey)) {
this.movingForward = true;
this._x += opposite*Math.cos(_rotation*(Math.PI/180))*2;
this._y += opposite*Math.sin(_rotation*(Math.PI/180))*2;
} else {
this.movingForward = false;
}
if (Key.isDown(thisDownKey)) {
this.movingBackWard = true;
this._x -= opposite*Math.cos(_rotation*(Math.PI/180))*2;
this._y -= opposite*Math.sin(_rotation*(Math.PI/180))*2;
} else {
this.movingBackWard = false;
}
if (Key.isDown(thisLeftKey)) {
this.turningLeft = true;
this._rotation -= 5;
} else {
this.turningLeft = false;
}
if (Key.isDown(thisRightKey)) {
this.turningRight = true;
this._rotation += 5;
} else {
this.turningRight = false;
}
}
onClipEvent (keyDown) {
if (Key.isDown(Key.UP)) {
_root.yspeed = _root.yspeed-1;
}
if (Key.isDown(Key.DOWN)) {
_root.yspeed = _root.yspeed+1;
}
}
onClipEvent (load) {
maxspeed = 20;
turnspeed = 5;
speed = 0;
acceleration = 1;
_rotation = 0;
}

////
onClipEvent (enterFrame) {
//Slow down slowly
if (speed<0) {
speed += acceleration;
} else if (speed>0) {
speed -= acceleration;
}
//Restrict to speed limit
if (speed<-maxspeed) {
speed = -maxspeed;
} else if (speed>maxspeed) {
speed = maxspeed;
}
//Update position based on rotation and speed
_x += Math.cos(_rotation/180*Math.PI)*speed;
_y += Math.sin(_rotation/180*Math.PI)*speed;
}
onClipEvent (keyDown) {
if (Key.isDown(Key.UP)) {
//speed up to go forwards
speed += acceleration;
}
if (Key.isDown(Key.DOWN)) {
//speed up to go backwards
speed -= acceleration;
}
if (Key.isDown(Key.RIGHT)) {
//turn right
this._rotation += turnspeed;
}
if (Key.isDown(Key.LEFT)) {
//turn left
this._rotation -= turnspeed;
}
}
onClipEvent (load) {
maxspeed = 20;
turnspeed = 5;
speed = 0;
acceleration = 1;
_rotation = 0;
}

Re: got the car game turning, now another hickup NSurveyor
9/13/2005 7:31:26 PM
Why do you have two different versions of code in the one movieclip? JUST use
the code I provided (remove the old code):

onClipEvent (enterFrame) {
//Slow down slowly
if(speed<0){
speed+=acceleration;
}else if(speed>0){
speed-=acceleration;
}
//Restrict to speed limit
if(speed<-maxspeed){
speed = -maxspeed;
}else if(speed>maxspeed){
speed = maxspeed;
}
//Update position based on rotation and speed
_x += Math.cos(_rotation/180*Math.PI)*speed;
_y += Math.sin(_rotation/180*Math.PI)*speed;
}
onClipEvent (keyDown) {
if (Key.isDown(Key.UP)) {
//speed up to go forwards
speed+=acceleration;
}
if (Key.isDown(Key.DOWN)) {
//speed up to go backwards
speed-=acceleration;
}
if (Key.isDown(Key.RIGHT)) {
//turn right
this._rotation += turnspeed;
}
if (Key.isDown(Key.LEFT)) {
//turn left
this._rotation -= turnspeed;
}
}
onClipEvent (load) {
maxspeed = 20;
turnspeed = 5;
speed = 0;
acceleration = 1;
_rotation = -90;
}

Re: got the car game turning, now another hickup NSurveyor
9/14/2005 12:00:00 AM
You know, I'm trying to help you... I have no reason to help you - I just chose
to. You should be thankful I'm trying - and it doesn't help to insult me.

Sorry, there was one error in the code, try:

onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
//speed up to go forwards
speed += acceleration;
} else if (Key.isDown(Key.DOWN)) {
//speed up to go backwards
speed -= acceleration;
} else {
//Slow down slowly
if (speed<0) {
speed += acceleration;
} else if (speed>0) {
speed -= acceleration;
}
}
if (Key.isDown(Key.RIGHT)) {
//turn right
this._rotation += turnspeed;
}
if (Key.isDown(Key.LEFT)) {
//turn left
this._rotation -= turnspeed;
}
//Restrict to speed limit
if (speed<-maxspeed) {
speed = -maxspeed;
} else if (speed>maxspeed) {
speed = maxspeed;
}
//Update position based on rotation and speed
_x += Math.cos(_rotation/180*Math.PI)*speed;
_y += Math.sin(_rotation/180*Math.PI)*speed;
}
onClipEvent (load) {
maxspeed = 20;
turnspeed = 10;
speed = 0;
acceleration = 2;
_rotation = -90;
}

And here's an example: http://www.geocities.com/saif7463/car.swf
Re: got the car game turning, now another hickup Skee-Lo
9/14/2005 1:09:32 AM
mate, ur code sux. at least my long code makes the car move up and down!!!!!!! this isn't helping mate
Re: got the car game turning, now another hickup Skee-Lo
9/14/2005 11:01:48 PM
sorry dude. i jus got all worked up and wasn't thinkin.

i jus has to be dne by friday and yeh,

Re: got the car game turning, now another hickup Skee-Lo
9/15/2005 12:00:00 AM
hello,

u have helped us heaps mate, this would top it off one last thing needs to be
looked at and if u could help one last time i will be forever in ur det!!!!!

the car follows the side, so in other words when u put ur foot on the
accelerator (hold the key down) it goes to the side. not on the front, is there
a way that i can swap the sode around, make the car turn on sides and go on the
front in u catch my drift, if u do i'll be very much suprised!!!!! if u can't
mate, don't worry what i got is the dogiest drift rally game in the world and
i'm happy cos i'm using an old rally car!!!!! ironic yes!

so u are a champion if u can help before tommorow our time (remember in Aus
different time thingy) if u can't no dramas.

so thankyou mate, sorry bout my insults, rock on and long live the Lancia
Delta Intergrale Evolution 2!!!!!!!!!
Re: got the car game turning, now another hickup NSurveyor
9/15/2005 10:37:04 AM
AddThis Social Bookmark Button