00:00
00:00

AS: Movement - Asteroids ship

5,569 Views | 33 Replies
New Topic Respond to this Topic

AS: Movement - Asteroids ship 2005-11-13 15:21:21


AS: Main
Works best with
I think this will work best with 24 FPS since that what I used.
And you need to have the default settings for width and height.
Introduction
There are plenty of movement scripts in the AS: Main thread but I searched it over but I couldn't finad a movement tutorial for movement of the ship from good ol' Asteroids. I hope this will qualify to the AS: Main thread. Enough introduction said.

Ship Variables
First off, make a ship MC. I suppose you know how to do a MC because if you don't you shouldn't read this until you have learned it. Write this AS to yout ship MC:
onClipEvent (load) {
thrust = 1;
decay = .97;
maxSpeed = 15;
}

This is the variables we'll use. Not to hard, right?

Moving
First place this little piece of AS below the onClipEvent(load) script:
onClipEvent (enterFrame) {
}

This is what will happen every frame according to the FPS.
Now we want our ship to move so we'll place this AS between the {}'s:
if (Key.isDown(Key.RIGHT)) {
_rotation += 10;
}

This will indicate so when you press right our ship will rotate right. So therefore this will make our ship being able to turn right but we want our little fella' to be able to turn left aswell, right? Thought so. Now place this AS below your if AS you just did:
if (Key.isDown(Key.LEFT)) {
_rotation -= 10;
}

This basiclly does the same thing except it turns left when holding left and not right.
If you test your movie by using CTRL + ENTER you'll see that the ship only can rotate left and right at the moment. We'll fix that now by making our ship be able to move forward. For this we'll use the UP key using this AS:
if (Key.isDown(Key.UP)) {
xSpeed += thrust*Math.sin(_rotation*(Math.PI/180));
ySpeed += thrust*Math.cos(_rotation*(Math.PI/180));
} else {
xSpeed *= decay;
ySpeed *= decay;
}

First the if action indicates that the UP key is pressed and calculating the rotation the ship has and will me move that way. The } else { action indicates when the UP key is not pressed and decreases speed instead of increases. The decay variable which decreases the speed you can change in the onClipEvent(load) action above if you want your ship to decrease speed faster or slower but I do reccomend letting it be like it is.
Now so the ship can't drive faster than the max speed we did in the variable maxSpeed before, here is the script for that:
speed = Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed))
;
if (speed>maxSpeed) {
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}

As I said above this will do so our ship can't drive faster than max speed.
Now for a really small but reallt important piece of script:
_y -= ySpeed;
_x += xSpeed;

This little AS makes our ship move based on the calculations meantioned above.

Coming out on other side same position
The above script is all that's necessary for the ship to move but for a real Asteroids ship we need the ship to come out on the other side if it reaches the end.
Example: If our ship moves our from the field to the right it will be moved to the left of the screen. This is the code we'll use for that:
if (_y<0) {
_y = 399;
}
if (_y>399) {
_y = 0;
}
if (_x<0) {
_x = 549;
}
if (_x>549) {
_x = 0;
}

I don't think I'll have to write an explanation for the script above since if you take this tutorial you should understand it... And I mentioned it above :P

Full code
The code is done now but for those who are too lazy to try to put the above scripts together here is the full code with explanation comments in //:
(May be a little disoriented because it can happen for some reasons here in the BBS)

onClipEvent (load) {
// only done once, therefore we'll put variables here so it won't update
thrust = 1;
decay = .97;
maxSpeed = 15;
}
onClipEvent (enterFrame) {
// rotate right or left
if (Key.isDown(Key.RIGHT)) {
_rotation += 10;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 10;
}
//
//
if (Key.isDown(Key.UP)) {
// calculate speed and way to move based on rotation
xSpeed += thrust*Math.sin(_rotation*(Math.PI/180));
ySpeed += thrust*Math.cos(_rotation*(Math.PI/180));
} else {
// indicates when the UP key is released
xSpeed *= decay;
ySpeed *= decay;
}
// maintain speed limit
speed = Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed))
;
if (speed>maxSpeed) {
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}
// move ship based on calculations above
_y -= ySpeed;
_x += xSpeed;
// moves the ship to other side when off-field
if (_y<0) {
_y = 399;
}
if (_y>399) {
_y = 0;
}
if (_x<0) {
_x = 549;
}
if (_x>549) {
_x = 0;
}
}

//guywithhiscomp


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-13 19:03:10


doesnt work and i made a whole tutorial on amkign an asteroids game. it just rotates, must be a problem withg the LOad part.

Response to AS: Movement - Asteroids ship 2005-11-13 20:43:45


wow thats a pretty nice tutorial even though u could have explained more for Teh noobs lol. 4/5


*User Page under construction*

BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-13 20:45:24


nvm what i just said 5/5 =P


*User Page under construction*

BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-14 01:38:29


At 11/13/05 07:03 PM, pyro111 wrote: doesnt work

It works for me. Maybe it's because I use Flash MX


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-14 01:43:14


its alright pretty basic

Response to AS: Movement - Asteroids ship 2005-11-14 02:24:01


Not bad. Pretty useful, actually. 8/10.


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-14 05:04:25


I'd say it's a car code you tried to explain :O


wat

Response to AS: Movement - Asteroids ship 2005-11-14 05:07:31


the code didnt work for me either


========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-14 11:44:25


neato. I was thinking about making a game (not asteroids) but with this movement style. Neat.

Response to AS: Movement - Asteroids ship 2005-11-15 19:45:12


At 11/14/05 01:38 AM, guywithhiscomp wrote:
At 11/13/05 07:03 PM, pyro111 wrote: doesnt work
It works for me. Maybe it's because I use Flash MX

i got flash mx 2004 and it still doesnt work. idk why. you should try to fix it to work on other comp. looks good though.

Response to AS: Movement - Asteroids ship 2005-11-15 19:50:36


i dont mean to accuse anyone of anything but thats the exact same code as the flash Mx Movement_thrust sample... but from your description it looks like you understand it so whatever


This blog I made | This game sucks | I, twit

Just trying to get enough Twitter followers to quit Facebook forever

Response to AS: Movement - Asteroids ship 2005-11-20 13:28:06


At 11/15/05 07:45 PM, pyro111 wrote: i got flash mx 2004 and it still doesnt work. idk why. you should try to fix it to work on other comp. looks good though.

because it's working in flash player 6 !!! ZOMG

Response to AS: Movement - Asteroids ship 2005-11-20 13:30:35


Zielak, when did you get permission from the higher authorities to say "ZOMG"?

You should be shot.

Good car game tutorial. :P 4.71/5


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.

Response to AS: Movement - Asteroids ship 2005-11-20 13:41:35


Response to AS: Movement - Asteroids ship 2005-11-20 13:53:25


At 11/20/05 01:30 PM, Devenger wrote: Zielak, when did you get permission from the higher authorities to say "ZOMG"?

what?? permission??
what do you mean??
is this rule protected??
if yes sorry... :((
*rofl. what do you mean*

Response to AS: Movement - Asteroids ship 2005-11-20 14:48:05


At 11/20/05 01:41 PM, BobRicci wrote: YOU STEALER!!!

Lol I didn't took it from there!!


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-20 14:54:11


At 11/20/05 02:48 PM, guywithhiscomp wrote: Lol I didn't took it from there!!

no, no, i do believe he has you there. if it werent for a few minor changes, id say it was a direct copy and paste. i mean, you clearly understand how the script works, but you definately copied it. there is far too much in common:

same variable names (exactly)
same values for variables (except the decay and the stage dimensions)
same PECULIAR notation for the same things:

speed = Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed))
;
if (speed>maxSpeed) {
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}

just small, consistant things. my vote goes with ricci on this one...


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-20 14:59:13


Lol, I got it from C:\Program\Macromedia\Flash MX\Samples\FLA


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-20 15:00:14


And I explained it so newbies could understand it


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-20 15:00:57


Then you still stole it :P

As punishment, you must design guns for my in-production txt-based game, slave!

muhahahhahaahaahhhhhahhhhhh!1!111"21!!!1!.


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.

Response to AS: Movement - Asteroids ship 2005-11-20 15:03:09


Ok... And it's not really stolen just explained :P

AS: Movement - Asteroids ship


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-20 15:03:33


At 11/20/05 02:59 PM, guywithhiscomp wrote: C:\Program\Macromedia\Flash MX\Samples\FLA

heh, i didnt even know that existed.
still... creating tutorials from premade engines is in bad taste.
but, nevermind my vote. no flaming for you!


BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-20 15:03:56


At 11/20/05 03:00 PM, Devenger wrote: Then you still stole it :P

As punishment, you must design guns for my in-production txt-based game, slave!

muhahahhahaahaahhhhhahhhhhh!1!111"21!!!1!.

hahahahah.... *roflrolf*

Response to AS: Movement - Asteroids ship 2005-11-20 15:06:11


At 11/20/05 03:00 PM, Devenger wrote: As punishment, you must design guns for my in-production txt-based game, slave!

text based guns? hmmm... sounds deliciously paradoxical
how does this work for you?

thats the best i can do. lol...

BBS Signature

Response to AS: Movement - Asteroids ship 2005-11-20 15:09:43


At 11/20/05 03:06 PM, authorblues wrote: [stupid unicode]

that was supposed to be a symbol shaped like this:
,--


BBS Signature

Response to AS: Movement - Asteroids ship 2006-01-09 21:56:53


Really nice tutorial. I've combined it with Denvish's 'Aim at Mouse' script and added some of my own to make a fun spaceship game. Now for collissions :P

Response to AS: Movement - Asteroids ship 2006-03-13 21:46:53


i was just informed, that for flash 8, you must include
xSpeed=0
ySpeed=0
when you are defining your variables.
just a heads up to the people whos ship was just rotating.
PS:info credited to -Vengeance-

Response to AS: Movement - Asteroids ship 2006-08-05 04:01:06


a very good tutorial. however in flash mx/8 (x/y)Speed don't appear to be valid functions. to get this to work in those versions switch the player from 7 to 6, it will work.

great script, i'm going to combine this with other scripts to hopefully make a little asteroids game.

Response to AS: Movement - Asteroids ship 2006-08-05 04:37:54


At 8/5/06 04:01 AM, _Vengeance_ wrote:

erm, why bump this really old thread?!?

also, change your username, it's confusing.

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature