Tải bản đầy đủ (.pdf) (14 trang)

Determining the Passage of Time

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (30.1 KB, 14 trang )


< Day Day Up >

Determining the Passage of Time
The getTimer() function returns (in milliseconds) the length of time elapsed since the
movie was first opened and played; therefore, if the movie has been open for six seconds,
the following script:

var playBackTime:Number = getTimer();


would assign playBackTime a value of 6000 (1,000 milliseconds for each second). This
is an accurate and precise representation of time based on the user's system clock and is
not dependent on the movie's frame rate, the user's processor speed, or whether the movie
is playing. This is also a universal value, which means that it represents the playback of
the movie as a whole; you cannot track the length of time for which individual timelines
have been present.
By setting a variable's value based on the value returned by the getTimer() function and
comparing that value to the value returned by the function at a later point in time, you can
evaluate the amount of time (too much or too little) that has passed within that span, and
take appropriate action. To better understand this principle, consider the following
example:

// Button A functionality

var startTime:Number;

buttonA_btn.onRelease = function() {

startTime = getTimer();


}

// Button B functionality

var nowTime:Number;

buttonB_btn.onRelease = function() {

nowTime = getTimer();

if (nowTime - startTime < 1000) {

message_txt.text = "You press buttons pretty quickly";

} else {

message_txt.text = "You're pretty slow";

}

}


Here, the action on one button (buttonA_btn) establishes a starting point by capturing the
time when the button is pressed and released. When buttonB_btn is pressed and released,
the time is captured again. A conditional statement is then used to determine whether the
amount of time between these two button presses was more or less than a second—and
then acts accordingly. Similarly, by using the following script, you can facilitate double-
clicking functionality for a button—that is, the script takes action only if separate clicks
occur within a half-second of each other.


var lastClick:Number;

myButton_btn.onRelease = function(){

if(getTimer() - lastClick < 500) {

// Actions

}

lastClick = getTimer();

}


By integrating the getTimer() function with the onEnterFrame event and some
conditional statements, you can create a mechanism that triggers actions at specific times,
independently of the timeline and with much greater accuracy. Look at the following
example:

slideShow.onEnterFrame = function() {

if (getTimer() > 5000) {

// Actions

}

if (getTimer() > 10000) {


// Actions

}

if (getTimer() > 15000) {

// Actions

}

}


This script triggers actions every five seconds while the movie plays. This is often the
most accurate way of executing actions at specific points in time during a movie's
playback.
In the following exercise, we'll use the getTimer() function to create a timer/alarm.
1. Open makeMyDay2.fla.
We'll continue building on the project from the preceding exercise. This time
around, however, we'll work on the Alarm section of the application. This section
of the project contains three NumericStepper component instances named minutes,
seconds, and loops, which control when the alarm goes off as well as how many
times the alarm sound plays.
Next to these NumericStepper instances is a Button component instance named
start_btn that starts the timer/alarm.
Above these elements is a movie clip instance named clock_mc, which includes
several moving elements (just like a real timer). Let's take a look at those.

2. Double-click the clock_mc movie clip instance to edit it in place.

This timeline contains two layers: the Graphic layer contains the main clock
graphic, and the Hands layer contains three movie clip instances that represent
various indicators on the clock face. The secondHand_mc movie clip represents
the clock's second hand, which rotates and simulates a tick each second the timer
is on. Just below this instance is a movie clip instance named minuteHand_mc,
which also rotates—but at
1
/10 the speed of the second hand (just as on a real
clock). The third movie clip instance appears as a small red tick mark at the top of
the clock. Named alarmHand_mc, this instance rotates when the timer is started, to
indicate when the alarm is set to go off.

The functionality of the Alarm section of the application will be handled by two
main functions: one for starting the alarm timer, and another for stopping the timer
and playing the alarm sound. All the script for this exercise will be placed on
Frame 1 of the application.
3. Return to the main timeline. With the Actions panel open, select Frame 1 of the
Actions layer and add the following script at the end of the current script:
4.
5. start_btn.onRelease = function (){
6.
7. alarmOn();
8.
9. }
10.

This step attaches an onRelease event handler to the start_btn instance. When this
event is triggered, the alarmOn() function (which we have yet to script) is called.
This function turns on the timer and activates the alarm.
4. Add the following script at the end of the current script:

5.
6. var startingTime:Number;
7.
8. function alarmOn(){
9.
10. }
11.

The first line creates a variable named startingTime, which eventually will hold a

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×