Author Topic: BlinkFX - for Flixel Power Tools  (Read 2152 times)

Servious

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
BlinkFX - for Flixel Power Tools
« on: Wed, Jul 31, 2013 »
Okay, so someone HAS to have made this before, but I can't find it, so I made one, and I'm posting it here.
Tell me if there's a better way to do this, but this seems to work great.

BlinkFX.as:
Code: [Select]
package org.flixel.plugin.photonstorm.FX
{
import mx.core.FlexSprite;
import org.flixel.FlxG;
import org.flixel.FlxSprite;
/**
* ...
* @author Servious
*/
public class BlinkFX extends BaseFX
{
private var _blinkInterval:Number;
private var _minAlpha:Number;
private var _maxAlpha:Number;
private var _fade:Boolean;
private var _fading:Boolean = true;
private var _counter:Number = 0;

public function BlinkFX()
{
}
/**
* A simple "blink" effect
* @param sprite The sprite you want blinking
* @param blinkInterval how long the whole blink cycle should take in seconds
* @param fade if it should fade while blinking, or just blink
* @param minAlpha the lowest alpha value possible
* @param maxAlpha the highest alpha value possible
*/
public function create(sprite:FlxSprite, blinkInterval:Number = 1, fade:Boolean = true, minAlpha:Number = 0, maxAlpha:Number = 1):void {
this._fade = fade;
this._maxAlpha = maxAlpha;
this._minAlpha = minAlpha;
this._blinkInterval = blinkInterval;
this.sprite = sprite;
if (_fade && this.sprite.alpha <= _minAlpha) {
_fading = false;
}
active = true;

}

public function draw():void {
if (ready) {
if (_fade) {

var factor:Number = ((_maxAlpha - _minAlpha) * 4) / (_blinkInterval / FlxG.elapsed);

if (_fading) {
sprite.alpha -= factor;
if (sprite.alpha <= _minAlpha) {
_fading = !_fading
}
} else {
sprite.alpha += factor;
if (sprite.alpha >= _maxAlpha) {
_fading = !_fading
}
}

} else {
_counter += FlxG.elapsed;
if (_counter >= _blinkInterval / 2) {
if (sprite.alpha <= _minAlpha) {
sprite.alpha = _maxAlpha;
} else {
sprite.alpha = _minAlpha;
}
_counter = 0;
}
}
}
}
}

}

and add this (and various imports) anywhere to FlxSpecialFX.as:

Code: [Select]
/**
 * Creates a blink Effect
 *
 * @return BlinkFX
 */
public static function blink():BlinkFX
{
var temp:BlinkFX = new BlinkFX

members[temp] = temp;

return members[temp];
}

BlinkFX.as has to go into org/flixel/plugin/photonstorm/FX

Here is an in-code example of usage:

Code: [Select]
FlxG.addPlugin(new FlxSpecialFX());
var pressEnter:FlxText = new FlxText(0, 0, FlxG.width, "PRESS ENTER");
var blink:BlinkFX = FlxSpecialFX.blink();
blink.create(_pressEnter, .25, true, .25);
blink.start();
add(blink.sprite);

That will create a FlxText that blinks from .25 alpha to 1 and back in .25 seconds!

Hope this helps someone!