Flash basic Theory Class 19th Chapter Practical Skill Ⅱ

Source: Internet
Author: User
Tags event listener

Back to "flash Basic Theory Class-Catalog"

Animation based on timer and time

All the examples of this book so far have been implemented by placing the motion code in the Onenterframe method and assigning it to a Enterframe event handler function. I've always thought this was the easiest way, because the concept of frames is ingrained in Flash, and it's for us; I guess most of us get used to it.

However, for those friends who are transferred from a non-Flash programming environment, it may not be customary for this pattern. For them, timing animation models (using interval or Timer) seem to control animations more precisely.

Later, we'll take a look at "Time based animation", a technique that can be used as a frame and as a timer.

Timer-based animations

As a key class used by the timer animation, it is Flash.utils.Timer, not surprisingly. At the same time we also need flash.events.TimerEvent class.

Using timers is actually no different from using Enterframe. All we need to do is create a timer, tell it how long it "ticks" and listen for Timerevent.timer events, just like listening to event.enter_frame events. Oh, and tell the timer when it's going to start! Next, the timer broadcasts a timed event every once in a while, and it processes the function that the call assigns to it. The interval that is triggered by the timer is specified in milliseconds when the timer is created. Let's look at a simple example (which can be found in timer1.as):

package {
 import flash.display.Sprite;
 import flash.utils.Timer;
 import flash.events.TimerEvent;
 public class Timer1 extends Sprite {
  private var timer:Timer;
  public function Timer1() {
   init();
  }
  private function init():void {
   timer = new Timer(30);
   timer.addEventListener(TimerEvent.TIMER, onTimer);
   timer.start();
  }
  private function onTimer(timer:TimerEvent):void {
   trace("timer!");
  }
 }
}

The important part is bold. We create a timer that tells it to fire every 30 milliseconds, meaning about 33 times per second. Adds a listener for an event and moves it. The OnTimer method is similar to the onenterframe we used before.

That's all we need to know about the timer. It also has two other beautiful features. One is when you create a timer by repeatcount the second argument, telling it how many times it will run. Let's say we're going to have the timer run once per second for a total of 5 seconds. You can do this:

timer = new Timer(1000, 5);

If you do not specify the number of repetitions or pass in 0, the timer will run indefinitely.

Another good thing is to have the timer start or stop at some point, just call timer.stop or Timer.start. In some cases, this is simpler than deleting and acceding to an event listener.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.