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
