topical media & game development

talk show tell print

#graphic-player-10-cube-be-nascom-flash-component-SimpleStatsView.ax

#graphic-player-10-cube-be-nascom-flash-component-SimpleStatsView.ax [swf] [flash] flex


  package be.nascom.flash.component{
          
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.system.System;
          import flash.text.TextField;
          import flash.text.TextFieldAutoSize;
          import flash.text.TextFormat;
          import flash.utils.getTimer;
          
          public class @ax-graphic-player-10-cube-be-nascom-flash-component-SimpleStatsView extends Sprite{
  
                  protected var _fps:int;                
                  public function get fps():int{
                          return _fps;
                  }
                  
                  protected var _memory:String;
                  public function get memory():String{
                          return _memory;
                  }
                  
                  protected var _last_frame_time:int;
                  protected var _current_frame_time:int;
                  
                  protected var _fps_field:TextField;
                  protected var _memory_field:TextField;
                  
                  protected var _bg_color:uint;
                  protected var _stats_color:uint;
                  
                  public function @ax-graphic-player-10-cube-be-nascom-flash-component-SimpleStatsView(bg_color:uint=0x000000,stats_color:uint=0xFFFFFF){
                          super();
                          _bg_color=bg_color;
                          _stats_color=stats_color;
                          init();
                  }
                  
                  public function init():void{
                          addEventListener(Event.ENTER_FRAME, tick);
                          this.graphics.beginFill(_bg_color,1);
                          this.graphics.drawRect(0,0,100,50);
                          this.graphics.endFill();
                          
                          var format:TextFormat = new TextFormat("Arial", 12, _stats_color, false, false, false);
                          
                          var fps_label:TextField = new TextField();
                          fps_label.selectable=false;
                          fps_label.autoSize = TextFieldAutoSize.LEFT;
                          fps_label.defaultTextFormat = format;
                          fps_label.text="FPS:";
                          addChild(fps_label);
                          
                          _fps_field = new TextField();
                          _fps_field.selectable=false;
                          _fps_field.x=fps_label.width+3;
                          _fps_field.width=100;
                          _fps_field.defaultTextFormat = format;
                          addChild(_fps_field);
                          
                          var memory_label:TextField = new TextField();
                          memory_label.selectable=false;
                          memory_label.autoSize = TextFieldAutoSize.LEFT;
                          memory_label.defaultTextFormat = format;
                          memory_label.text="Mem:";
                          addChild(memory_label);
                          
                          _memory_field = new TextField();
                          _memory_field.selectable=false;
                          _memory_field.x=memory_label.width+3;
                          _memory_field.autoSize = TextFieldAutoSize.LEFT;
                          _memory_field.defaultTextFormat = format;
                          addChild(_memory_field);        
                          
                          _memory_field.y=memory_label.y=20;
                          _fps_field.height=fps_label.height=_memory_field.height=memory_label.height=20;
                  }
  
                  protected function tick(event:Event):void{
                          _current_frame_time = getTimer();
                          _fps = 1000/(_current_frame_time - _last_frame_time);
                          _last_frame_time = _current_frame_time;
                          _memory=(System.totalMemory/1024/1024).toFixed(2);
                          update();
                  }
                  
                  protected function update():void{
                          _fps_field.text = String(_fps);        
                          _memory_field.text = _memory+ "MB";        
                  }
                  
          }
  }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.