Stage.frameRate
. It allows you to change the number of updates to the program window per second on the fly. In previous versions of ActionScript, we had significant problems with the implementation of this feature in the integrated development environment (IDE). Fortunately, times have changed and now complex programs do not "hang", lingering in the computer's memory in the background.
NativeApplication
Event.ACTIVATE
and Event.DEACTIVATE
; these events increase the number of framerates when the program is used (Active) and decreases when it is inactive (Idle). With a single empty window, the results showed a processor load of 1.8% in usage mode and 0.4% in idle mode. In general, you can set the frequency of updating the program window to 0.01 or turn it off, then the program takes only 0.2% of the total CPU load, while I found out that the chrome window does not lose focus.
package {
import flash.desktop.NativeApplication;
import flash.display.Sprite;
import flash.events.Event;
public class Application extends Sprite {
public function Application () {
__init ();
}
private function __init ():void {
NativeApplication.nativeApplication.addEventListener
(Event.ACTIVATE, __activate__);
NativeApplication.nativeApplication.addEventListener
(Event.DEACTIVATE, __deactivate__);
}
private function __activate__
($event:Event):void {
stage.frameRate = 50;
}
private function __deactivate__ ($event:Event):void {
stage.frameRate = 1;
}
}
}
package {
import flash.desktop.NativeApplication;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.getTimer;
public class Application extends Sprite {
public static const ACTIVE:int = 50;
public static const INACTIVE:int = 1;
public var active:Boolean;
public var scrolling:Boolean;
public var buffer:int;
public function Application () {
__init ();
}
private function __init ():void {
NativeApplication.nativeApplication.addEventListener
(Event.ACTIVATE, __activate__);
NativeApplication.nativeApplication.addEventListener
(Event.DEACTIVATE, __deactivate__);
stage.addEventListener
(MouseEvent.MOUSE_WHEEL, __mouseWheel__);
}
private function __activate__ ($event:Event):void {
active = true;
stage.frameRate = ACTIVE;
}
private function __deactivate__ ($event:Event):void {
active = false;
stage.frameRate = INACTIVE;
}
private function __mouseWheel__ ($event:MouseEvent):void {
if (!active) {
if (!scrolling) {
stage.addEventListener
(Event.ENTER_FRAME, __enterframe__);
}
stage.frameRate = ACTIVE;
scrolling = true;
buffer = getTimer () + 500;
}
}
private function __enterframe__
($event:Event):void {
if (buffer < getTimer ()) {
stage.frameRate = INACTIVE;
scrolling = false;
stage.removeEventListener
(Event.ENTER_FRAME, __enterframe__);
}
}
}
}
animate()
to call at the beginning of each movement. You may want to integrate your own fps retarder into your tweet engine, so you will no longer need to manually call animate()
.
package {
import flash.desktop.NativeApplication;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getTimer;
public class Application extends Sprite {
public static const ANIMATING:int = 50;
public static const ACTIVE:int = 24;
public static const INACTIVE_VISIBLE:int = 5;
public static const INACTIVE_INVISIBLE:int = 1;
public var active:Boolean;
public var animating:Boolean;
public var buffer:int;
public function Application () {
__init ();
}
private function __init ():void {
NativeApplication.nativeApplication.addEventListener
(Event.ACTIVATE, __activate__);
NativeApplication.nativeApplication.addEventListener
(Event.DEACTIVATE, __deactivate__);
}
public function activate ():void {
if (!animating) {
stage.frameRate = ACTIVE;
}
}
public function deactivate ():void {
if (!animating) {
stage.frameRate = (stage.nativeWindow.visible) ?
INACTIVE_VISIBLE : INACTIVE_INVISIBLE;
}
}
public function animate ($duration:int = 1000):void {
stage.frameRate = 50;
buffer = getTimer () + $duration;
animating = true;
if (!animating) {
stage.addEventListener (Event.ENTER_FRAME, __checkBuffer__);
}
}
private function __activate__ ($event:Event):void {
active = true;
activate ();
}
private function __deactivate__ ($event:Event):void {
active = false;
deactivate ();
}
private function __checkBuffer__ ($event:Event):void {
if (buffer < getTimer ()) {
stage.removeEventListener
(Event.ENTER_FRAME, __checkBuffer__);
animating = false;
if (active) {
activate ();
} else {
deactivate ();
}
}
}
}
}
Source: https://habr.com/ru/post/91961/