PDA

View Full Version : SpaceShip Movement



BallR0507
June 5th, 2004, 12:07 AM
Ok, Im New With Flash And Im Building A Gta Like Game,But The Problem Is That,My Player Can Rotate,But I Dont Know How To Make Him Move In That Direction,So Basically He Just Sits There.What Should I Do?Im Totally confused??

vinnyramone
June 5th, 2004, 12:12 AM
ive done that game before what you have to do is make the background move instead of the car i think if you go to www.friendsofed.com you can download fla's that do exactly what you need i just cant remember which chapter it is. heres the link: http://www.friendsofed.com/books/1590592360/code.html

yhack
June 5th, 2004, 06:37 AM
well...

make a movie clip called direction
in the first make him facing up
in 2nd him down
3rd left
and 4th right



// go down
onClipEvent (enterFrame) {
if (Key.isDown(Key.DOWN)) {
gotoAndPlay("direction", 2);
this._Y += 5;
}
}
// go up
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
gotoAndPlay("direction", 1);
this._y -= 5;
}
}
// go right
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
gotoAndPlay("direction", 4);
this._x += 5;
}
}
// go left
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
gotoAndPlay("direction", 3);
this._x -= 5;
}
}


put this man u want 2 move :)

yhack

tofu
June 5th, 2004, 09:13 AM
Well if you are comfortable with triginometry you should use flash's Math.cos and Math.sin to move your character, youll also need to use radians. btw yhack i think he wants is full 360 degree movesment not 4 directions.

BallR0507
June 5th, 2004, 09:48 AM
Well if you are comfortable with triginometry you should use flash's Math.cos and Math.sin to move your character, youll also need to use radians. btw yhack i think he wants is full 360 degree movesment not 4 directions.Yes I Have Learned About The Sin,Cos,And Tan,But Never Really Understood it My Teacher Told Us To Use A Calclator And Thats It,Plus I Never Expected To Actually Use It!

tofu
June 5th, 2004, 10:26 AM
Flash can be your calculator.

1st. Convert your degrees into radians:
(I think this is the right formula, not sure.)

radians = movieClip._rotation * (Math.PI/2);

2nd. Movement.

if(Key.isDown(Key.UP)){
movieClip._x += Math.sin(radians) * speed;
movieClip._y += Math.cos(radians) * speed;
}

replace speed variable with wanted speed.

Note:I might have sin and cos mixed up. Is sin x axis? Im a little rusty on my trig.

KGKraeer
June 7th, 2004, 02:12 PM
Found this at Flashkit. Basically, make a movieclip in the shape of your ship, car, or whatever. Give it an instance name and apply he following:

onClipEvent (load) {
thrust = 1;
decay = .99;
maxSpeed = 15;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
_rotation += 10;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 10;
}
if (Key.isDown(Key.UP)) {
xSpeed += thrust*Math.sin(_rotation*(Math.PI/180));
ySpeed += thrust*Math.cos(_rotation*(Math.PI/180));
flames._visible = 1;
} else {
xSpeed *= decay;
ySpeed *= decay;
flames._visible = 0;
}
speed = Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed));
if (speed>maxSpeed) {
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}
_y -=ySpeed;
_x +=xSpeed;
if (_y<0) {
_y = 400;
}
if (_y>400) {
_y = 0;
}
if (_x<0) {
_x = 550;
}
if (_x>550) {
_x = 0;
}
}


The last several lines can be removed. Basically they just use the movie size (550x400 in this case) to tell the ship to reappear on the opposite side of the stage if it goes too far off like the original 'asteroids' ship. You could replace them with code that'll move the background around depending on the clip's speed.

Hope this helps!

tofu
June 8th, 2004, 04:49 AM
KGKs script will work fine for the car your character will drive. But dont use it on the person himslef or itll look weird. ;)

Update: looking KGKs script tells me that the real formula for degrees to radians is:
radians = degrees*(Math.PI/180);