Author Topic: [Mod: v1.47+] Advanced Flicker Settings  (Read 2085 times)

Markham

  • Guest
[Mod: v1.47+] Advanced Flicker Settings
« on: Wed, Dec 23, 2009 »
This adds more functionality to Flixel's flicker function.  I've had parts implemented since 1.27, and I don't remember the affected functions changing much between versions in any critical sort of way, so I think this should work for versions earlier than 1.47 as well.  This is coded so that pre-existing calls to the flicker function in your code will still work as before.

public function flicker(Duration:Number=1,Frequency:Number=-1,Pulse:Number=0,Phase:Number=0):void
Duration - Unchanged.  0 for infinite, -#'s to stop.
Frequency - The number of full on/off cycles per second.
Pulse - Pulse width: adjusts ratio between on and off states in a cycle. 1 = always on, -1 = always off.  See demonstration for details.
Phase - Phase shift: adjusts the wave offset.  See demonstration for details.

The demonstration can be found here since the forum's Flash tags just create a link to the file.


Open FlxCore.as
Find:
Code: [Select]
protected var _flickerTimer:Number;Add after:
Code: [Select]

//@desc Advanced flicker settings
private var _flickerElapsed:Number;
private var _flickerFrequency:Number;
private var _flickerPulseWidth:Number;


Find:
Code: [Select]

//@desc Just updates the flickering. FlxSprite and other subclasses override this to do more complicated behavior.
virtual public function update():void
{
...

if(flickering())
{
...
}
Replace:
Code: [Select]
if(flickering())
{
(everything in this 'if' statement)
}
with:
Code: [Select]
if(flickering())
{
if(_flickerTimer > 0)
{
_flickerTimer -= FlxG.elapsed;
if(_flickerTimer == 0)
_flickerTimer = -1;
}
if(_flickerTimer < 0)
flicker(-1);
else
{
_flickerElapsed += FlxG.elapsed;
if (_flickerFrequency == -1 || (visible && _flickerFrequency*(1+_flickerPulseWidth) <= _flickerElapsed) || (!visible && _flickerFrequency*(1-_flickerPulseWidth) <= _flickerElapsed)) {
_flickerElapsed -= _flickerFrequency*((visible)?1+_flickerPulseWidth:1-_flickerPulseWidth);
_flicker = !_flicker;
visible = !_flicker;
}
}
}


Find:
Code: [Select]

//@desc Tells this object to flicker for the number of seconds requested (0 = infinite, negative number tells it to stop)
public function flicker(Duration:Number=1):void { _flickerTimer = Duration; if(_flickerTimer < 0) { _flicker = false; visible = true; } }
Replace the entire function with this one:
Code: [Select]
//@desc Tells this object to flicker for the number of seconds requested (0 = infinite, negative number tells it to stop)
//@param Frequency Number of full on/off cycles per second
//@param Pulse Cycle pulse width
//@param Phase Wave offset in degrees
public function flicker(Duration:Number=1,Frequency:Number=-1,Pulse:Number=0,Phase:Number=0):void {
while (Phase < 0) Phase += 360;
while (Phase >= 360) Phase -= 360;
if (Frequency <= 0) Frequency = -1;
_flickerTimer = Duration;
_flickerFrequency = 1/Frequency;
_flickerPulseWidth = Pulse;
_flickerElapsed = _flickerFrequency*Phase/180;
if(_flickerTimer < 0)
{
_flicker = false;
visible = true;
}
}
« Last Edit: Wed, Dec 23, 2009 by Markham »