|
|||
| week 05a - exercises, enterframe event, flags, movieclip states, events | |||
apply techniques : |
event handling, variables, if statements, movieclips This section is designed to apply some of the techniques we've been learning in the last few weeks:
We'll build up a simple application where a button turns the rotation of a movieclip on or off.
|
|
| enterframe event : | make something happen every frame The framerate of a Flash (or Director) application provides a very convenient timing mechanism to trigger repeated actions such as animating by code. It is also useful for checking the value of a button, sensor, mouse position, or implementing any system of tracking the current state of things. To make a movieclip animate every frame, we apply a standard event handler to the movieclip with the onClipEvent method. In this example, we rotate the movieclip by 10 degrees every frame.
Code on the movieclip: every frame, rotate the clip by 10 degrees
|
|
| flags/if statements : | a system for communicating in code - one part of the code signals another part of the code Variables can be used in code as a way for one part of the code to communicate with another part of the code. These signaling variables are often called Flags, because they are used like the flags on ships are used to communicate information to other ships (n.b. in both shipping and programming a more formal name for this signaling is the semaphore). For example, a button can set a flag variable to be true or false. Separately, a movieclip can check the value of this flag variable every frame (using the above enterframe event). If the movieclip finds the flag to be set to "true" using an if statement, the movie clip can rotate by 10 degrees, otherwise it does nothing.
Code on the first frame of the timeline: create and initialize the flag variable
Code on the button: toggle the flag value between true and false
Code on the square movieclip: check the value of the flag, rotate if it is true
|
|
| movieclip states : | using parts of the timeline in a movieclip for different states Because movieclips enable you to encapsulate a set of graphics and an associate timeline into a single object, it's often convenient to use this capability for maintaining different states of a graphic object in different frames of its own timeline. For example, you might put all of the associated graphics for a spaceship in one movieclip. One frame could have the normal ship graphic, another frame could have the ship plus the flame coming out of the back, and another frame could have the ship exploded. Depending on if state of the ship is stopped, moving, or exploding, the code will switch the frame of the movie clip to be displayed. In our current example, we are using the timeline of the button (actually a movieclip) to place one frame that displays "off", and another frame to display "on" for the respective states of the button.
The code for the first frame of the main movie and the square movieclip are the same as above. Code for the button: toggle the flag, and change the button's visual state by switching to different frames
Timeline of the button: the "off" labeled frame contains the off graphics/text, the "on" frame contains the on graphics/text. Code for frame 1: stop the movie so we don't cycle back and forth between the two frames
|
|
interface example : |
using these concepts for interface elements The simple but powerful techniques described above can be used in many different applications. A common one is for the manipulation of interface elements. In the below example, news clips are displayed in compact headline format until clicked on. They then enlarge to show the full article. If clicked again, they shrink down to the just the headline. There are two basic states to the movieclips, the condensed one and the expanded one, which are represented in two corresponding frames in the movieclip. But an additional intermediate stated is created in this application to display the transition between one state and another. This transition state is accomplished using the enterframe event to animate the expansion or contraction of the movieclip.
Code on the movieclip event load: initialize the scale, size flag, and scale transition flag
Code on the movieclip event enterframe: check to see if the user clicked, if so, scale up/down by 20% every frame using "+= 20" until we reach the maximum or minimum scale (check with an if statement), then shut off the scaling by reseting the clicked flag. Switch the timeline frame as appropriate for the new state using gotoAndStop. Toggle the size flag appropriately.
Code on the movieclip event release: when the user clicks, set the clicked flag to true so the scaling will start.
|
|
exercise : |
apply the above techniques Create an application where a button toggles the random motion of an object on the screen.
Additional challenges:
|
|
|
|
| all materials on this web site © copyright 2003, Philip van Allen |
top |