Flash Timing Mechanisms
syllabus | schedule | assignments | Web Resources | ARTC courses
[Flash Intro]     [Drawing]     [Other Tools]     [Modify/Import]     [Symbols]     [Animation]     [Navigation]     [Publishing]    [Audio]     [ActionScript]     [Textboxes]     [Conditionals/Loops]     [Functions]     [Timing]     [Interaction]     [KeyPressed]    
Timing
  • setInterval( function, milliseconds ) - starts timer
  • clearInterval() - stops timer

EX: timer used for delay

  • One MC with instance name: circle
  • All actions on frame 1:

function moveDot(){ // function controlling MC

circle._x = Math.floor(Math.random() * 500); // circle = instance name

}

myTimer = setInterval(moveDot, 1000);
// setInterval is contained in the variable myTimer, so it can be cleared later

download - flash doc

 

EX: starting and stopping timer

  • One MC with instance name: circle
  • Two buttons: one with instance name - red, the other with instance name - blue
  • All actions are on frame 1 (no scripts on buttons in this ex):

stop(); // pause for user to click button
var myTimer; // var controlling interval

function moveDot(){ // function controlling MC

circle._x = Math.floor(Math.random() * 450); // circle = instance name

}

red.onRelease = function(){ // callback style function; red = the red button's instance name

myTimer = setInterval(moveDot, 500); // start timer

}; // callbacks have a semicolon after the closing bracket

blue.onRelease = function(){ // callback style function; blue = the blue button's instance name

clearInterval(myTimer); // stop timer

}; // callbacks have a semicolon after the closing bracket

download - flash doc

 

EX: counter

  • Create 3 DYNAMIC text boxes:
    • Dynamic text #1 - Instance Name = minutes, right justification
    • Dynamic text #2 - Instance Name = zero
    • Dynamic text #3 - Instance Name = seconds, right justification
  • Line up the text boxes as shown in the diagram:
    • Notice that zero overlaps seconds (this is for formatting purposes)
    • A fourth STATIC text box contains the colon.

  • All actions on frame 1:

minutes.text = 0; // initialize
seconds.text = 0;

function counter() {

seconds.text++; // increment seconds
if (seconds.text == 60) { // if seconds equal 60...

minutes.text++; // add to minutes
seconds.text = 0; // reset seconds

}

if (seconds.text < 10) { // if seconds less than 10...

zero.text = 0; // placeholder 0 to help with visual format of counter

} else {

zero.text = " "; // placeholder equal empty quotes to help with visual format of counter

}

}

myTimer = setInterval(counter, 1000); // timer running every second

download - flash doc

 

EX: starting, stopping and resetting counter

  • Counter from above example
  • Three buttons with instance names: red, blue and yellow
  • All actions on frame 1:

minutes.text = 0; // initialize
seconds.text = 0;

function counter() {

seconds.text++; // increment seconds
if (seconds.text == 60) { // if seconds equal 60...

minutes.text++; // add to minutes
seconds.text = 0; // reset seconds

}

if (seconds.text < 10) { // if seconds less than 10...

zero.text = 0; // placeholder 0 to help with visual format of counter

} else {

zero.text = " "; // placeholder equal empty quotes to help with visual format of counter

}

}

red.onRelease = function() { // red button starts timer

clearInterval(myTimer);
myTimer = setInterval(counter, 1000);

};

blue.onRelease = function() { // blue button stops timer

clearInterval(myTimer);

};

yellow.onRelease = function() { // yellow button resets timer

minutes.text = 0;
seconds.text = 0;
zero.text = 0;

};

download - flash doc