PDA

View Full Version : goal = (racecar + mouse directional) + movement



kpxnamja
June 2nd, 2006, 02:03 AM
Hello, I've been working on a project where the upper body is controlled via the mouse, and the lower body down is controlled by the directional keys w,a,s,d. Here's the dilema: I managed to get the w,a,s,d keys to work; however, it doesn't act like a car. I wish to make the movement to go foward when "W" is pressed regardless if it points north,east,west,and south, etc. vise-versa with "s". And for "a" and "d" move counter wise /clock wise

help is much appreciated.

And also as a bonus, it would be really nice if all my actionscript could be replaced from a clip event to a script, all done in one frame.

Thanks :D

For those who hate to d/l fla's

this is the clip event on the characters "body"

onClipEvent (enterFrame) {
x=this._xmouse;
y=this._ymouse*-1;
angle = Math.atan(y/x)/(Math.PI/180);
if(x<0){angle+=180}
if(x>=0&&y<0){angle+=360}
_root.angletext=angle;
_root.arrow._rotation=angle*-1;

//Basic movement
if (Key.isDown(87)){
_y -= _root.speed;
_root.arrow._y -=_root.speed;
}
if (Key.isDown(83)){
_y += _root.speed;
_root.arrow._y +=_root.speed;
}
if (Key.isDown(65)){
_x -= _root.speed;
_root.arrow._x -=_root.speed;
}
if (Key.isDown(68)){
_x += _root.speed;
_root.arrow._x +=_root.speed;
}
}

Egon
June 2nd, 2006, 06:06 AM
in your 1st frame set your speed to 0.

and replace your script with the following:


onClipEvent (enterFrame) {

x = this._xmouse;
y = this._ymouse*-1;
angle = Math.atan(y/x)/(Math.PI/180);
if(x<0){angle+=180}
if(x>=0&&y<0){angle+=360}
_root.angletext=angle;

_root.arrow._rotation= (angle*-1)+this._rotation;

//Basic movementonClipEvent (enterFrame) {

x = this._xmouse;
y = this._ymouse*-1;
angle = Math.atan(y/x)/(Math.PI/180);
if(x<0){angle+=180}
if(x>=0&&y<0){angle+=360}
_root.angletext=angle;
// Add own rotation to correct the pointing angle
_root.arrow._rotation= (angle*-1)+this._rotation;

//Basic movement
//This code will advance the car forward.
if (Key.isDown(Key.UP)) {
_root.speed += 1;
}
// This will make the car go backwards
if (Key.isDown(Key.DOWN)) {
_root.speed -= 1;
}
//The car will start to slow down after the speed of 25
if (Math.abs(_root.speed)>25) {
_root.speed *= .6;
}
// This will change the angle of the car
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
}
_root.speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*_root.speed;
y = Math.cos(_rotation*(Math.PI/180))*_root.speed*-1;
if (!_root.move.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
_root.speed *= -.6;
}
// Set x and y of arrow
_parent.arrow._x = this._x;
_parent.arrow._y = this._y;
}


credits for those who deserve it, the movement script comes from:
http://www.flashkit.com/tutorials/Games/Creating-Matthew_-835/index.php

SacrificialLamb
June 2nd, 2006, 06:37 AM
the arrow code stopped working when I did it so I redid a bit of that, it was looking needlessly complicated (I did not remove the code just /**/ed it) but it looked like it was giving a value to some where else that might need to be redone coz the value might be inverted now
but the movement thing works




onClipEvent (enterFrame) {
x=this._xmouse;
y=this._ymouse;
angle=(Math.atan2(this._y-_root._ymouse, this._x-_root._xmouse)*180/Math.PI+180)
/*angle = Math.atan(y/x)/(Math.PI/180);
if(x<0){angle+=180}
if(x>=0&&y<0){angle+=360}*/
_root.angletext=angle;
_root.arrow._rotation=angle;

//Basic movement
ro=this._rotation* Math.PI/180
moox = (Math.cos(ro))*_root.speed
mooy = (Math.sin(ro))*_root.speed
if (Key.isDown(87)){ //W ford
_y += mooy;
_x += moox;
}
if (Key.isDown(83)){// S back
_y -= mooy;
_x -= moox;
}
if (Key.isDown(65)){// A CCW
this._rotation-=9
}
if (Key.isDown(68)){//D CW
this._rotation+=9
}
_root.arrow._x=_x
_root.arrow._y=_y
}

[edit] no credits needed I made my own code