This is topic Flash 2008 Help in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=047651

Posted by Blayne Bradley (Member # 8565) on :
 
Okay I have a car and I want it to move forward, turn in either direction and when I move forward the shape moves in the direction its pointed.

How do I do this codewise.

I have a shape, I program it to turn left or right based on keypress.

And now I need it to move in the direction its pointed.

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

I think is the lines needed to get it done according to the teacher but it isnt working right.

Can anyone help?
 
Posted by fugu13 (Member # 2859) on :
 
Okay, do you understand what those lines are doing? Apparently car_mc._rotation is a variable holding the rotation direction in degrees. By converting to radians (that's the * Math.PI/180 stuff), you then take the cosine (or sine) to get the part of the direction in the x (or y) direction. Having this, you multiply it by the Acceleration to find the acceleration in each direction (x and y).

I'm not sure why these acceleration numbers are then assigned to car_mc._x and car_mc._y . I would think those were positions. If those are positions, you need to do something like the following:
code:
car_mc._velocity_x += Math.cos( car_mc._rotation * Math.PI/180 ) * Acceleration

car_mc._velocity_y = Math.sin( car_mc._rotation * Math.PI/180 ) * Acceleration

car_mc._x += car_mc._velocity_x
car_mc._y += car_mc._velocity_y

That is, first you update the velocity of the car (whatever that member variable is called), then you update its position based on that velocity. Of course, all this should really be abstracted out, but whatever.
 
Posted by Blayne Bradley (Member # 8565) on :
 
Okay I figured out the problem, aparently the symbol needs to be pointed in a particular direction first and then rotated to the proper direction for it to work.
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2