Author Topic: Blinking.  (Read 943 times)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Blinking.
« on: Sat, Jun 2, 2012 »
Hi,

I want to have my ammo text blinking when player is low on ammo. I wanted to use a timer to toggle color but it will be kinda messy cause I will have to have two timers and each have to check the ammo inside them and turn on and off the timer based on current ammo.

Do you know any other solution for blinking stuff in flixel?

-tnx
blog, twitter, Check out my award winning game, Rot Gut:

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Blinking.
« Reply #1 on: Sat, Jun 2, 2012 »
I'm quite fond of this method. Create a new class that extends whatever you want to blink (eg in your case FlxText). Then I put the blinking logic inside that class so that it always blinks, and then I simply turn active on/off to control the blinking. For example

Code: [Select]
package 
{
import org.flixel.FlxText;
import org.flixel.FlxG;

public class BlinkingText extends FlxText
{
private const BLINK_RATE:Number = 3; // blink X times per second
private var blinkCounter:Number;

public function BlinkingText(X:Number, Y:Number, Width:Number, Text:String)
{
super(X, Y, Width, Text);
}

override public function update():void
{
super.update();

if (blinkCounter >= 1 / BLINK_RATE)
{
// blink!
blinkCounter = blinkCounter - 1 / BLINK_RATE;
visible = !visible;
}
blinkCounter += FlxG.elapsed;
}

public function resetBlinking():void
{
blinkCounter = 0;
visible = true;
}

}

}

Then in play state or wherever

Code: [Select]
// start blinking
if (FlxG.keys.justPressed("X"))
{
myBlinkingText.resetBlinking();
myBlinkingText.active = true;
}

// stop blinking
if (FlxG.keys.justPressed("C"))
{
myBlinkingText.resetBlinking();
myBlinkingText.active = false;
}
« Last Edit: Sat, Jun 2, 2012 by wg/funstorm »

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Blinking.
« Reply #2 on: Sat, Jun 2, 2012 »
Thanks, it works perfectly. I just had to make the text parameter optional and put active=false in the c'tor.
blog, twitter, Check out my award winning game, Rot Gut: