If you want to follow along as you read, you can download the flash source from lesson 2 (click right and select save file). When you open the document, you will see something like this:
You can test the movie (control->Test Movie) and you will see the rocket orbiting the earth. You can rotate the rocket left and right and you can turn on the engine, but engine has no effect because no thrust model has been implemented.
Let get that done. Add the following code to the onClipEvent(enterFrame) function:
function vsensed() { if(plume1._visible) { theta = _rotation/57.3; cs = Math.cos(theta); sn = Math.sin(theta); dvx = thrust*dt*sn; dvy = -thrust*dt*cs; } else { dvx = 0.0; dvy = 0.0; } }Next, change the following lines:
v = v + dt*ax; u = u + dt*ay;to this:
vsensed(); v = v + dt*ax + dvx; u = u + dt*ay + dvy;Now, in the onClipEvent(load) function, initialize the thrust scaling variable and add a call to vsensed to the end of the function.
thrust = .001; vsensed();This initializes the sensed velocity (delta-v) variables.
Test the movie. Rotate the rocket until the nose is pointed in the direction of motion, then turn on the jet. You can then fly the rocket away from the earth. If you fly too far, you will fly right off the screen, so, let's put in a zoom command before we quit.
After line 7, add the following:
eW = _root.earth._width; eH = _root.earth._height; sw = _width; sh = _height; zoom = 1.0; lock = 0;This code saves the original width and height of the earth and the rocket ship clips. The variable zoom is initialzed to 1. This variable will be used to scale the graphics and the distances on the screen when we zoom out or in. We created a lock variable to temporarily lock out the zoom command so we won't zoom too fast.
Now we need a function to rescale everything for the selected zoom factor. Add this function to the onClipEvent(enterFrame) method:
function zoom_view() { _root.earth._width = eW/zoom; _root.earth._height = eH/zoom; _root.earth._x = W/2 - _root.earth._width/2; _root.earth._y = H/2 - _root.earth._height/2; rot = _rotation; _rotation = 0; _width = sw/zoom; _height = sh/zoom; _rotation = rot; lock = 10; }
Now add these lines right before the propagate() call:
if(lock > 0) { lock = lock - 1; } if(Key.isDown(88) and lock == 0) { // zoom out zoom = zoom + 1; zoom_view(); } if(Key.isDown(90) and lock == 0) { // zoom in if(zoom > 1) { zoom = zoom - 1.0; } zoom_view(); }Finally, inside the function "propagate()," we need to change these lines:
_x = km2pix*xs + W/2; _y = km2pix*ys + H/2;to this:
_x = km2pix*xs/zoom + W/2; _y = km2pix*ys/zoom + H/2;Now you can zoom out as much as necessary to see the rocket as it moves away from earth. Test the movie and see that if you hit the "X" key, you can zoom out, and if you hit the "Z" key, you can zoom back in.
Since things are going so well, let's go ahead and add a little eye candy to the flash video. I think it would look cool to have a starry background scrolling by. You can find some good candidates on the internet, or just use this one: .
Just click right and save the image. You need to create a movie clip for the background, so let's create a new movie clip symbol and call it "outerspace." With the outspace clip opened for editting, import the "space.png" bit map to the stage. Create a new level in the main time line and drop two instances of the "outerspace" movie clip into it. Name them space2 and space1. Locate space2 at (-1024,0) and space1 at (0,0).
In clip space1, add the following actionscript:
onClipEvent(enterFrame) { _x = (_x+1)%1024; }and in space2, add the folowing actionscript:
onClipEvent(enterFrame) { _x = _x + 1; if(_x == 0) { _x = -1024; } }Now test movie. You steer and maneuver, you can zoom out and in, and you have a nice starry background in the view. Let's call it day and the end of lesson 3.