Simple Stage framrate measuring class : Animation Utilities « Animation « Flash / Flex / ActionScript

Flash / Flex / ActionScript
1. Animation
2. Array
3. Class
4. Data Type
5. Development
6. Function
7. Graphics
8. Language
9. Network
10. Regular Expressions
11. Statement
12. String
13. TextField
14. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Flash / Flex / ActionScript » Animation » Animation Utilities 
Simple Stage framrate measuring class
 
/*
 * hexagon framework - Multi-Purpose ActionScript 3 Framework.
 * Copyright (C) 2007 Hexagon Star Softworks
 *       __    __
 *    __/  \__/  \__    __
 *   /  \__/HEXAGON \__/  \
 *   \__/  \__/ FRAMEWORK_/
 *            \__/  \__/
 *
 * ``The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 */
package com.hexagonstar.util.debug
{
  import flash.display.Stage;
  import flash.events.Event;
  import flash.events.EventDispatcher;
  import flash.events.TimerEvent;
  import flash.utils.Timer;
  
  /**
   * Simple Stage framrate measuring class.
   */
  public class FPSMeter extends EventDispatcher
  {
    // Properties /////////////////////////////////////////////////////////////////
    
    public static const FPS_UPDATE:String = "fpsUpdate";
    
    private var _stage:Stage;
    private var _timer:Timer;
    private var _fps:int;
    private var _isRunning:Boolean;
    
    // Constructor ////////////////////////////////////////////////////////////////
    
    /**
     * Constructor
     */
    public function FPSMeter(stage:Stage)
    {
      _stage = stage;
      _fps = 0;
      _isRunning = false;
    }
    
    // Public Methods /////////////////////////////////////////////////////////////
    
    /**
     * Starts FPS polling.
     */
    public function start(pollInterval:uint = 1000):void
    {
      if (!_isRunning)
      {
        _isRunning = true;
        _timer = new Timer(pollInterval, 0);
        _timer.addEventListener(TimerEvent.TIMER, onTimer);
        _stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        _timer.start();
      }
    }
    
    /**
     * Stops FPS polling.
     */
    public function stop():void
    {
      if (_isRunning)
      {
        _isRunning = false;
        _timer.stop();
        _timer.removeEventListener(TimerEvent.TIMER, onTimer);
        _stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        _timer = null;
      }
    }
    
    /**
     * Returns the current FPS.
     
     @return The currently polled frames per second.
     */
    public function getFPS():int
    {
      return _fps;
    }
    
    // Private Methods ////////////////////////////////////////////////////////////
    
    /**
     * Called on every Timer event.
     * @private
     */
    private function onTimer(event:TimerEvent):void
    {
      dispatchEvent(new Event(FPSMeter.FPS_UPDATE));
      _fps = 0;
    }
    
    /**
     * Called on every EnterFrame event.
     * @private
     */
    private function onEnterFrame(event:Event):void
    {
      _fps++;
    }
  }
}

        
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.