当用户在浏览器中切换标签时,为什么flash应用程序暂停?如何防止?,我需要让它连续播放
更新
<?xml version="1.0"?>
<s:Module
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init();"
xmlns:MyComp="components.*">
<fx:Script>
<![CDATA[
import library.TicketCard;
import library.UtilFunctions;
[Embed(source="../images/ball1.png")]
private var red:Class;
[Embed(source="../images/ball2.png")]
private var orange:Class;
[Embed(source="../images/ball3.png")]
private var blue:Class;
[Embed(source="../images/ball4.png")]
private var green:Class;
[Embed(source="../images/ball5.png")]
private var purple:Class;
private var numTimer:Timer;
private var step:int;
public var currentRand:Number;
public var randNumbers:Array;
public var vNum:Array;
public var speedAnim:Number;
[Bindable]
public var countNum:int = 0;
public var ticketsBought:*;
private function init():void {
vNum = new Array();
step = 1;
ticketsBought = parentApplication.m_ticket.child.ticketsBought;
speedAnim = 1000;
generateNumbers();
randNumbers = new Array();
for(var i:int = 0; i < 90; i++)
{
randNumbers.push((i + 1));
}
numTimer = new Timer(1000, 180);
numTimer.addEventListener(TimerEvent.TIMER, onTick);
numTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTickComplete);
initialPosition();
numTimer.start();
}
private function initialPosition():void {
bigBall.width = 4;
bigBall.height = 4;
bigBall.horizontalCenter = -92;
bigBallLabel.horizontalCenter = bigBall.horizontalCenter;
bigBallLabel.setStyle('fontSize', 1);
}
private function onTickComplete(e:TimerEvent):void {
parentApplication.showMessage('end game!');
}
private function onTick(e:TimerEvent):void {
if(step == 1)
{
if(countNum < 90)
{
countNum++;
}
var rand:Number = UtilFunctions.randomRange(randNumbers.length - 1, 0);
currentRand = randNumbers[rand];
for(var i:int = 0; i < ticketsBought.length; i++)
{
var ticket:* = ticketsBought[i];
var numbers:Array = ticket.ticketNumbers;
for(var j:int = 0; j < numbers.length; j++)
{
if(numbers[j].text == currentRand)
{
var num:* = numbers[j];
num.setStyle('backgroundColor', '#552c68');
num.setStyle('color', '#FFFFFF');
ticket.checkedNumbers++;
break;
}
}
}
randNumbers.splice(rand, 1);
var n:* = vNum[rand];
n.alpha = 1;
vNum.splice(rand, 1);
bigBallLabel.text = currentRand.toString();
startIncreaseAnim();
}
else
if(step == 2)
{
startReduceAnim();
}
numTimer.stop();
}
private function generateNumbers():void {
for(var i:int = 0; i < 90; i++)
{
var m:MyImg = new MyImg();
var c:Canvas = new Canvas();
var l:Label = new Label();
l.text = (i + 1).toString();
l.horizontalCenter = 0;
l.verticalCenter = 0;
c.width = 25;
c.height = 25;
c.alpha = 0.3;
vNum.push(c);
m.horizontalCenter = 0;
m.verticalCenter = 0;
if(i >= 0 && i < 18)
{
m.source = red;
}
else if(i >= 18 && i < 36)
{
m.source = orange;
}
else if(i >= 36 && i < 54)
{
m.source = blue;
}
else if(i >= 54 && i < 72)
{
m.source = green;
}
else if(i >= 72 && i < 90)
{
m.source = purple;
}
c.addChild(m);
c.addChild(l);
numbersBlock.addChild(c);
}
}
public function startIncreaseAnim():void {
stage.addEventListener(Event.ENTER_FRAME, increaseAnim);
}
public function startReduceAnim():void {
stage.addEventListener(Event.ENTER_FRAME, reduceAnim);
}
public function endIncreaseAnim():void {
stage.removeEventListener(Event.ENTER_FRAME, increaseAnim);
}
public function endReduceAnim():void {
stage.removeEventListener(Event.ENTER_FRAME, reduceAnim);
}
public function increaseAnim(e:Event):void {
bigBall.width += 4;
bigBall.height += 4;
bigBall.horizontalCenter += 4;
bigBallLabel.horizontalCenter = bigBall.horizontalCenter;
bigBallLabel.setStyle('fontSize', bigBallLabel.getStyle('fontSize') + 1);
if(bigBall.horizontalCenter >= 0)
{
step = 2;
endIncreaseAnim();
numTimer.start();
}
}
public function reduceAnim(e:Event):void {
bigBall.width -= 4;
bigBall.height -= 4;
bigBall.horizontalCenter += 4;
bigBallLabel.horizontalCenter = bigBall.horizontalCenter;
bigBallLabel.setStyle('fontSize', bigBallLabel.getStyle('fontSize') - 1);
if(bigBall.horizontalCenter >= 90)
{
step = 1;
initialPosition();
endReduceAnim();
numTimer.start();
}
}
]]>
</fx:Script>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
</fx:Style>
<mx:VBox height="727" width="176" styleName="leftBar" verticalGap="4">
<mx:Box verticalGap="12">
<mx:Box width="176" horizontalScrollPolicy="off">
<mx:Canvas width="100%" height="100" id="bigBallWrapper" horizontalScrollPolicy="off">
<MyComp:MyImg id="bigBall" verticalCenter="0" horizontalCenter="0" source="@Embed(source='../images/ball1-1.png')"/>
<s:Label color="#000000" id="bigBallLabel" fontSize="24" text="45" verticalCenter="0" horizontalCenter="0" />
</mx:Canvas>
<mx:Canvas width="100%">
<s:Label text="ball # {countNum}" fontSize="18" verticalCenter="0" horizontalCenter="0" />
</mx:Canvas>
</mx:Box>
<MyComp:MyImg id="bingoLeft" source="@Embed(source='../images/bingoLeft.png')"/>
</mx:Box>
<mx:Tile fontSize="12" fontFamily="bor" color="black" direction="vertical" horizontalGap="9" height="100%" width="100%" paddingLeft="8" paddingRight="8" id="numbersBlock" verticalScrollPolicy="off" horizontalScrollPolicy="off">
</mx:Tile>
</mx:VBox>
</s:Module>
有点复杂的代码:),但是计时器在第一次执行,然后停止,在每个动画启动之后,我将它用作setTimeout(在js中)
答案 0 :(得分:1)
以下是我根据我创建的一个简单应用程序做出的一些观察:
Timer
处理程序Event.ENTER_FRAME
处理程序FlexEvent.UPDATE_COMPLETE
属性)graphics
)有趣的观察,粗体,底部的代码。
测试#1
当Flash应用的浏览器标签位于“前景”时,这些<s:Rect />
和“enterFrame”事件会按预期工作。 Timer
以指定的时间间隔触发,Timer
尽快调度(我的机器每30-50毫秒)。
当Flash应用的浏览器标签位于“后台”时,大约每500毫秒调度一次Event.ENTER_FRAME
。如果将Event.ENTER_FRAME
的间隔设置为低于500毫秒,则Timer
也将仅每500毫秒触发一次。
正如我们猜测的那样,当Flash Player知道它不可见时,它会尝试通过较少频率触发Timer
来减少使用CPU,并且不能使用小于500毫秒的Event.ENTER_FRAME
间隔。< / p>
测试#2
但是当你尝试更新屏幕时会发生什么?
我让应用在Timer
处理程序中绘制了一排圆圈。屏幕更新按预期发生,但Flash应用程序的浏览器选项卡在后台时速度更慢。
测试#3
但是Flex怎么样? Flex组件有自己的生命周期,上面的绘图代码不参与该生命周期。让我们为Event.ENTER_FRAME
添加一个事件处理程序。这是一个在每个Flex组件生命周期结束时调度的事件(提交任何更改的属性,测量和呈现的一次迭代)。在此测试中,除非您执行调整浏览器窗口大小(导致Flex重绘屏幕)等操作,否则不会调度该事件。
如果Flash应用的浏览器标签位于“后台”,则不会调度FlexEvent.UPDATE_COMPLETE
(调整浏览器窗口大小时)。 Flash Player知道该应用不可见,因此当我调整浏览器大小时,它不会浪费CPU尝试重绘屏幕。
测试#4
好的,现在我觉得我们已经准备好尝试做一些与您在应用程序中所做的事情接近的事情......为某事物的位置设置动画。在这种情况下,我将通过在“enterFrame”处理程序中更改它的x / y坐标来移动FlexEvent.UPDATE_COMPLETE
。
当Flash应用的浏览器标签位于“后台”时,屏幕似乎仍在更新,但速度要慢得多。另请注意,当矩形移动时,<s:Rect />
也被派遣了。
<强>结论强>
根据上述证据,Flash Player 不暂停应用程序,而是当Flash知道应用程序不可见时限制应用程序。
现在这对你的代码意味着什么?我还不确定:)也许你的更新正在发生,但它似乎很慢,似乎没有任何改变?
CODE:
FlexEvent.UPDATE_COMPLETE