【问题标题】:AS3 - Make the screen flash for half a secondAS3 - 让屏幕闪烁半秒
【发布时间】:2014-07-03 18:04:23
【问题描述】:

我想做的是:

在与 [object] 碰撞后,我希望屏幕闪烁大约半秒。我试过for loopswhile loops 但它们似乎不起作用。我不知道我应该如何编程。

自从我开始制作游戏以来,我一直在尝试弄清楚如何做到这一点,因此如果有人可以帮助我,那将会很有帮助。

感谢您的阅读。

【问题讨论】:

    标签: actionscript-3 flash effects


    【解决方案1】:

    你需要使用一些涉及时间的东西。循环都在一个不会暂停时间的线程中运行 - 这就是它们不起作用的原因。

    以下是您如何使用 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();
    }
    

    您可以使用setIntervalsetTimeout、Tweening 库和ENTER_FRAME 事件处理程序来执行此操作。

    【讨论】:

    • 函数里没有的东西,有多少可以放到函数里?
    • 如果你真的想的话,你可以把它全部放在一个函数中(对 tick 和 timerDone 方法使用内联函数)。你想让我重写它,把它全部封装到一个函数中吗?
    • 如果你这样做了,那将非常有用,对你非常好。
    • 我确实做到了。呼叫flashScreen() 将闪烁屏幕。调整50 值,使其闪烁得更快或更慢。
    • 非常感谢 ^u^ 你的救命稻草。
    猜你喜欢
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 2015-11-05
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多