【发布时间】:2014-07-03 18:04:23
【问题描述】:
我想做的是:
在与 [object] 碰撞后,我希望屏幕闪烁大约半秒。我试过for loops 和while loops 但它们似乎不起作用。我不知道我应该如何编程。
自从我开始制作游戏以来,我一直在尝试弄清楚如何做到这一点,因此如果有人可以帮助我,那将会很有帮助。
感谢您的阅读。
【问题讨论】:
标签: actionscript-3 flash effects
我想做的是:
在与 [object] 碰撞后,我希望屏幕闪烁大约半秒。我试过for loops 和while loops 但它们似乎不起作用。我不知道我应该如何编程。
自从我开始制作游戏以来,我一直在尝试弄清楚如何做到这一点,因此如果有人可以帮助我,那将会很有帮助。
感谢您的阅读。
【问题讨论】:
标签: actionscript-3 flash effects
你需要使用一些涉及时间的东西。循环都在一个不会暂停时间的线程中运行 - 这就是它们不起作用的原因。
以下是您如何使用 AS3 Timer 执行此操作(假设此代码在您确定发生冲突后立即运行)
function flashScreen():void {
var timer:Timer = new Timer(50, 10); //run the timer every 50 milliseconds, 10 times (eg the whole timer will run for half a second giving you a tick 10 times)
var flash:Shape = new Shape(); //a white rectangle to cover the whole screen.
flash.graphics.beginFill(0xFFFFFF);
flash.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
flash.visible = false;
stage.addChild(flash);
timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
//we've told AS3 to run this every 50 milliseconds
flash.visible = !flash.visible; //toggle visibility
//if(Timer(e.currentTarget).currentCount % 2 == 0){ } //or you could use this as a fancy way to do something every other tick
});
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
//the timer has run 10 times, let's stop this flashing madness.
stage.removeChild(flash);
});
timer.start();
}
您可以使用setInterval、setTimeout、Tweening 库和ENTER_FRAME 事件处理程序来执行此操作。
【讨论】:
flashScreen() 将闪烁屏幕。调整50 值,使其闪烁得更快或更慢。