Be a Supporter!

AS: Movement - Asteroids ship

  • 4,394 Views
  • 34 Replies
New Topic Respond to this Topic
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
AS: Movement - Asteroids ship Nov. 13th, 2005 @ 03:21 PM Reply

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
Pyromaniac
Pyromaniac
  • Member since: Jan. 14, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 13th, 2005 @ 07:03 PM Reply

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

JD77
JD77
  • Member since: Mar. 30, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 13th, 2005 @ 08:43 PM Reply

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
JD77
JD77
  • Member since: Mar. 30, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 13th, 2005 @ 08:45 PM Reply

nvm what i just said 5/5 =P


*User Page under construction*

BBS Signature
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 14th, 2005 @ 01:38 AM Reply

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
shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 14th, 2005 @ 01:43 AM Reply

its alright pretty basic

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 14th, 2005 @ 02:24 AM Reply

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


BBS Signature
Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS: Movement - Asteroids ship Nov. 14th, 2005 @ 05:04 AM Reply

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


wat
a compessor wil raise the volume while lowering the db - chronamut

Vengeance
Vengeance
  • Member since: Mar. 18, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 14th, 2005 @ 05:07 AM Reply

the code didnt work for me either


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

BBS Signature
Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: Movement - Asteroids ship Nov. 14th, 2005 @ 11:44 AM Reply

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

Pyromaniac
Pyromaniac
  • Member since: Jan. 14, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 15th, 2005 @ 07:45 PM Reply

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.

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to AS: Movement - Asteroids ship Nov. 15th, 2005 @ 07:50 PM Reply

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

Zielak
Zielak
  • Member since: Sep. 21, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 01:28 PM Reply

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

Devenger
Devenger
  • Member since: Dec. 24, 2004
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 01:30 PM Reply

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.

EmoNarc
EmoNarc
  • Member since: May. 1, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Zielak
Zielak
  • Member since: Sep. 21, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 01:53 PM Reply

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*

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 02:48 PM Reply

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

Lol I didn't took it from there!!


BBS Signature
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 02:54 PM Reply

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
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 02:59 PM Reply

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


BBS Signature
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 03:00 PM Reply

And I explained it so newbies could understand it


BBS Signature
Devenger
Devenger
  • Member since: Dec. 24, 2004
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 03:00 PM Reply

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.

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 03:03 PM Reply

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

AS: Movement - Asteroids ship


BBS Signature
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 03:03 PM Reply

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
Zielak
Zielak
  • Member since: Sep. 21, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 03:03 PM Reply

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*

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 03:06 PM Reply

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
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Movement - Asteroids ship Nov. 20th, 2005 @ 03:09 PM Reply

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

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


BBS Signature
railspider
railspider
  • Member since: Dec. 15, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to AS: Movement - Asteroids ship Jan. 9th, 2006 @ 09:56 PM Reply

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

PCpr0gramer
PCpr0gramer
  • Member since: Feb. 4, 2006
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Movement - Asteroids ship Mar. 13th, 2006 @ 09:46 PM Reply

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-

Vengeance-1
Vengeance-1
  • Member since: Jun. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Movement - Asteroids ship Aug. 5th, 2006 @ 04:01 AM Reply

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.

Vengeance
Vengeance
  • Member since: Mar. 18, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Movement - Asteroids ship Aug. 5th, 2006 @ 04:37 AM Reply

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