Get Things MovingNow we need to code our init frame, this will be where we initialise all our variables, and place the movieclips on the screen. In the timeline, right-click on frame 5 and select Add Blank Keyframe. Do this on both the Action layer and the Screen layer. Change the name in the frame label to init. Any time you need to add some new ActionScript code to a frame, you have to create a keyframe. If you simply add a frame (ie. NOT a keyframe) then the ActionScript code from the previous keyframe will be run when the playhead reaches that frame. If you click on frames 2, 3 or 4 you will see that it contains the code from frame 1 (ie. the previous keyframe). A blank keyframe simply means all the author time content you have added will be removed. This will have the effect of clearing the screen (ie. the splash screen on frame 1). We will add our code for the trampoline first. Add the following code in our init frame (frame 5) on the Actions layer: var mcTrampoline:MovieClip; This code is adding a movieclip (called trampoline_mc in the library) at depth 1, and assigning a reference to the instance as mcTrampoline. Now we can change the properties of the trampoline using the reference. eg.
Next we need to write our main loop code, so click on frame 6 and insert a keyframe and change the frame label to main (do not make this a blank keyframe - we don't want any author-time content we might want to add into the init frame to be removed!), then add the following code. This code will be run at 30fps (or whatever fps speed you have set you movie to): // main program loop This code will simply read the keyboard, and move the trampoline left or right, stopping before it reaches the edge of the screen. Finally, on frame 7, insert another keyframe and type the following code. Label this frame as endmain: gotoAndPlay("main");Lastly, add a frame at frame 7 on the screen layer, so that each layer has the same number of frames.
If you run the Flash movie now, you should see our trampoline moving left and right using the cursor keys.
( categories: )
|
User login |