Neon flickering effect in AS3 tutorial

After trawling the Web for various bits of information and tutorials for various ideas I have noticed how amazingly, so many tutorials are still being created for AS2 applications considering the power and superiority of AS3 so I have decided to convert a few of them to AS3 format, partly for myself and for content for this blog but also because I feel that it is important that more of the people that are still clinging onto the old AS2 days start to embrace AS3 and realise how simple to use it really is.

So, I came across this when doing a project for someone, who requested an effect and directed me towards an AS2 tutorial to outline the effect he was looking for.

The desired effect was to produce a neon text, flickering on and then off in a realistic fashion. This is all done in Flash CS3 / CS4.

Firstly the artwork needs to be produced. As flash is vector based it is best and simplest to produce the images in flash itself. This is very simple as it is only text broken apart into shapes. There are just two movie clips the first is the text in its “off” state. So we need to create a new symbol by selecting “Insert / New Symbol” or by pressing Ctrl or Opt / F8. Give it a simple name like neon_off or something. Firstly choose the text tool and in the centre of the stage add the necessary text. Then press Ctrl or Opt / B to break it apart once and separate the letters, then again to break it apart and turn it into shapes. Once this is done you can then with the stroke tool add a stroke to the edge of each of the letters with the settings illustrated.

Now we need to produce the the “neon on” text, this is done in the same way but we need to give the the text a coloured stroke and a white fill so that when the filters are applied it looks like a glowing neon light. Now these MovieClips along with the sound file which will be used when the text is flickering on need to be given a linkage so that they can be modified via ActionScript. This is done by selecting the MovieClips in the library, right clicking and selecting the MovieClip and ticking the box “Export for ActionScript” and giving it a class name.....as illustrated.

Now its time for the fun......the ActionScript.........

In AS3 the script is usually done in a separate file which the FLA links to at compile time and brings the script into the swf. So if we open a new ActionScript file and call it Flicker. We then start the file by the opening Package declaration and by importing the necessary libraries ......

Actionscript:
  1. package{
  2.  
  3.     import flash.display.*;
  4.     import flash.utils.*;
  5.     import flash.events.*;
  6.     import flash.filters.*;
  7.     import flash.media.*;
  8.  
  9.     public class Flicker extends MovieClip{

We then need to create the variables needed in AS3 as each instance is first created as a variable and then addressed or added in a function. Notice the variables which create instances of the MovieClips we created earlier.

Actionscript:
  1. private var timerOne:Timer;
  2. private var timerTwo:Timer;
  3. private var maxFlker:Number = .85;
  4. private var sound:Sound = new Neon();
  5. private var soundControl:SoundChannel = new SoundChannel();
  6. private var textOff:MovieClip = new Text_MC();
  7. private var textOn:MovieClip = new Texton_MC();
  8. private var glow:GlowFilter = new GlowFilter(0xFF00FF, 1, 20, 20, 2, 3, false,    false);
  9. private var blur:BlurFilter = new BlurFilter(5, 5, 2);

Now we are going to instantiate the timer which was firstly created as a variable and add an event listener to listen for the completion of the timer and then trigger the flickering this is done like so:

Actionscript:
  1. private function timerInit():void {
  2.     textOn.alpha = 0;
  3.     timerOne = new Timer(2000, 0);
  4.     timerOne.addEventListener(TimerEvent.TIMER, timerOn);
  5.     timerOne.start();
  6.     }
  7. private function timerOn(e:TimerEvent):void {
  8.     soundOn();
  9.     timerOne.stop();
  10.     timerOne.removeEventListener(TimerEvent.TIMER, timerOn);
  11.     addEventListener(Event.ENTER_FRAME, flickerOn);
  12.     }

Now for the flicker effect. At the moment the coloured MovieClip is on top of the grey one but it is set at an alpha of zero, once the timer has triggered its listener will add and Enter_Frame event listener which does what it says on the tin....each frame it will trigger its function and the function is as follows:

Actionscript:
  1. private function flickerOn(e:Event):void {
  2.     var ultraAlpha:Number = Math.random() * .9;
  3.     textOn.alpha = ultraAlpha;
  4.     if(ultraAlpha> maxFlker){
  5.         textOn.alpha = 1;
  6.         soundControl.stop();
  7.         removeEventListener(Event.ENTER_FRAME, flickerOn);
  8.         addEventListener(Event.ENTER_FRAME, allFlickerOn);
  9.         addtimerTwo();
  10.         }
  11.     }

What this does is each frame give the alpha of the coloured MovieClip a random alpha, once the alpha reaches a certain level, in this case almost the highest figure it can create, the enter frame listener is removed and another one created whilst the text flickers at a higher alpha ( to give the effect of being on and flickering whilst being on) and another timer is triggered which waits to switch the flickering off and start all over again......Finally of course before we finish we need to make sure that the document class of the fla is set to target the Flicker.as, which is done by adding the document class name in the document properties tab, by just clicking on the stage and opening properties.

As usual all source files and FLA are available here.

You can leave a response, or trackback from your own site.

10 Responses to “Neon flickering effect in AS3 tutorial”

  1. Many thanks for the tremendous idea! I would follow the blog, lot of interesting things. And my blog on science, I hope you too, like ;)

  2. Good story. Waiting for new articles. :)

  3. Dali says:

    This is very interesting !

    But I also would like to change the glow color to yellow (light bulb effect) instead of purple. Where do I change that ?

    Thank you !

  4. UltraVisual says:

    Hi Dali,
    In the flicker.as file there is a glow filter applied on line 18, the first parameter for this is the color of the glow which is currently set to 0xFF00FF which is a pink glow, just change this colour to a different hexidecimal color and it will change the color of the glow for you. For more details on glowfilters go to: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/
    Thanks
    Shane

  5. Dali says:

    Brilliant !
    Thank you !

  6. UltraVisual says:

    Hi
    It would be very simple, you would just find the point you wanted to end it, namely at the end of a timer function and instead of making it loop back to the beginning add a tween effect to fade the text out to nothing…..if you need any help just email me your files and I’ll adjust it for you.

    Shane

  7. Gaz says:

    This is brilliant, exactly what I’m looking for….but how do you turn the flash file into a hyperlink?? Is it possible?

  8. Levin says:

    Hi.
    Great tutorial, only I am a noob with flash so I don’t understand a thing about the first step.
    For me you are going too fast.
    Make this into a shape, press CTRL and F8 for symbol….
    I mean, what do I have to make into a symbol?
    Step by step tutorials for noobs like me are much easier to understand.

    Great affort never the less

  9. UltraVisual says:

    Hi Yes it is best practice to add any assets in flash as a symbol or movieclip, this enables the asset to be easily reusable, but also so that you can add the neccessary filters to them which can’t be done to simple shapes. I have not created this tute and aimed it solely at noobs but at people who have at least a simple understanding of flash and suggest that maybe you study a little more about the basics of flash before moving on to more intermediate things……
    Shane

  10. xtomx says:

    Nicely done. Question, do you think it is good practice to abbreviate variable names? Instead of “maxFlker,” why not “maxFlicker”? It’s not like AS charges by the letter.

Leave a Reply

Powered by WordPress | AT&T Cell Phones for Sale at iFreeCellPhones.com | Thanks to PalmPreBlog.com, MMORPGs and Online Shopping