AS3 提示类

package {
 //
 import flash.display.Sprite;
 import flash.events.KeyboardEvent;
 import flash.events.Event;
 import flash.text.TextField;
 import flash.text.TextFieldAutoSize;
 import flash.ui.Keyboard;
 import flash.utils.getTimer;
 import flash.system.System;
 //
 public class documentClass extends Sprite {
  //
  var active:Boolean = false;
  var allLogos:Array = new Array();
  var Counter:TextField = new TextField();
  var currentTime:int = 0;
  var FPS:TextField = new TextField();
  var Instructions:TextField = new TextField();
  var Memory:TextField = new TextField();
  var noOfLogos:int = 100;
  //
  public function documentClass():void {
   stage.addEventListener(KeyboardEvent.KEY_UP, keyListener);
   showHUD();
  }  
  //
  public function decreaseLogos(thousand:Boolean = false):void {
   if (!active) return;
   stopTest();
   if (thousand) {
    if (noOfLogos > 1000) {
     noOfLogos = noOfLogos-1000;
    }
   } else {
    if (noOfLogos > 100) {
     noOfLogos = noOfLogos-100;
    }
   }
   startTest();
  }
  //
  public function increaseLogos(thousand:Boolean = false):void {
   if (!active) return;
   stopTest();
   if (thousand) {
    noOfLogos = noOfLogos 1000;
   } else {
    noOfLogos = noOfLogos 100;
   }
   startTest();
  }
  //
  public function keyListener(event:KeyboardEvent):void {
   if (event.keyCode == Keyboard.UP) startTest();
   if (event.keyCode == Keyboard.DOWN) stopTest();
   if (event.keyCode == Keyboard.RIGHT) {
    if (event.ctrlKey == true) {
     increaseLogos(true);
    } else {
     increaseLogos();
    }
   }
   if (event.keyCode == Keyboard.LEFT) {
    if (event.ctrlKey == true) {
     decreaseLogos(true);
    } else {
     decreaseLogos();
    }
   }
  }
  //
  public function mainLoop(event:Event):void {
   var n:int;
   for (n=0;n<noOfLogos;n ) {
    var currentLogo:Logo = allLogos[n];
    currentLogo.updatePosition();
   }
   updateHUD();
  }
  //
  public function showHUD():void {
   FPS.text = "FPS";
   Counter.text = "Objects";
   Counter.x = 50;
   Memory.text = "mem";
   Memory.x = 200;
   var instText:String = "<font size='16'><b><u>Instructions</u></b>";
   instText = "\nUp - starts the test";
   instText = "\nDown - stops the test";
   instText = "\nRight - increases the number of logos by 100";
   instText = "\nLeft - decreases the number of logos by 100";
   instText = "\nCtrl-Right - increases the number of logos by 1000";
   instText = "\nCtrl-Left - decreases the number of logos by 1000";
   Instructions.htmlText = instText;
   Instructions.x = 275;
   Instructions.y = 170;
   Instructions.autoSize = TextFieldAutoSize.CENTER;
   stage.addChild(FPS);
   stage.addChild(Counter);
   stage.addChild(Memory);
   stage.addChild(Instructions);
  }
  //
  public function startTest():void {
   if (active) return;
   var n:int;
   for (n=0;n<noOfLogos;n ) {
    var myLogo:Logo = new Logo(stage.stageWidth/2, stage.stageHeight/2);
    allLogos[n] =  myLogo;
    stage.addChild(myLogo);
   }
   stage.addEventListener(Event.ENTER_FRAME, mainLoop);
   active = true;
   Instructions.y = -200;
   Counter.text = "Objects :" noOfLogos.toString();
  }
  //
  public function stopTest():void {
   if (!active) return;
   stage.removeEventListener(Event.ENTER_FRAME, mainLoop);
   var n:int;
   for (n=0;n<noOfLogos;n ) {
    stage.removeChild(allLogos[n]);
    var currentLogo:Logo = allLogos[n];
    currentLogo = null;
   }
   active = false;
   Instructions.y = 170;
  }
  //
  public function updateHUD():void {
   var fps:Number = 1/((getTimer()-currentTime)/1000);
   currentTime = getTimer();
   FPS.text = "FPS: " Math.round(fps).toString();
   Memory.text = "Mem :" (Math.round((System.totalMemory/1048576)*10)/10).toString() " MB";
  }
 }
}

 
 
posted @ 2008-10-23 10:02  Landy_di  阅读(98)  评论(0)    收藏  举报