作为一个flash项目,帧数是固定的,假如设置帧数为60fps,则stage的EnterFrame事件每秒会执行60次
有时候高速运算是需要的,但对于一些位图操作,如果高速渲染,则会造成CPU负载加大;在场景里自动运行的元件,通常都是在EnterFrame里渲染的,所以这里要根据情况控制渲染频率
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.setInterval;
public class Sample0915 extends Sprite
{
private var count:int;
private var rect:Sprite;
private var i:int;
private var fps:int = 40;
public function Sample0915()
{
//元件
rect = new Sprite();
rect.graphics.beginFill(0x0000FF);
rect.graphics.drawRect(0,0,50,50);
rect.graphics.endFill();
this.addChild(rect);
//定义一个5s的定时器
setInterval(stopEnterFrame,5000);
stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
i++;
if(i%2==0) //fps=60/2=30
// if(i%3==0) //fps=60/3=20
// if(i%4==0) //fps=60/4=15
{
render();
}
}
private function stopEnterFrame():void
{
stage.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
private function render():void
{
rect.x += 1;
count++;
trace(count);
}
}
}
{
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.setInterval;
public class Sample0915 extends Sprite
{
private var count:int;
private var rect:Sprite;
private var i:int;
private var fps:int = 40;
public function Sample0915()
{
//元件
rect = new Sprite();
rect.graphics.beginFill(0x0000FF);
rect.graphics.drawRect(0,0,50,50);
rect.graphics.endFill();
this.addChild(rect);
//定义一个5s的定时器
setInterval(stopEnterFrame,5000);
stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
i++;
if(i%2==0) //fps=60/2=30
// if(i%3==0) //fps=60/3=20
// if(i%4==0) //fps=60/4=15
{
render();
}
}
private function stopEnterFrame():void
{
stage.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
private function render():void
{
rect.x += 1;
count++;
trace(count);
}
}
}