Be a Supporter!

AS: Flicker effect

  • 3,048 Views
  • 6 Replies
New Topic Respond to this Topic
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
AS: Flicker effect May. 7th, 2006 @ 04:39 AM Reply

AS:Main

I didn't see a tutorial on this in AS:Main already, and since it's an effect which can be useful quite often in both games and animations I thought I'd try my hand at it.

This will cover a number of ways of a simble, frame rate based visibility flicker (something moving rapidly from visible to invisible). It's all pretty straightforward but there are quite a few ways to go about it.

NB: You may want to declare any variables used in this tutorial. Feel free.

The most basic way of achieving this is as follows. Place this code in the movie clip you want to flicker:

onClipEvent(enterFrame){
// Tells the code to execute every frame
if(_visible){
_visible = false;
}
// Checks if the clip is visible, and renders it invisible if it is
else{
_visible = true;
}
// Vice versa
}

Easy, eh? You can also make the entire animation flicker from the main timeline:

onEnterFrame = function(){
if(_visible){
_visible = false;
}
else{
_visible = true;
}
}

Of course, the flicker is fairly pointless and annoying if you're just going to have it for the entire duration of the movie clip's existance. To turn the effect on and off, just insert an extra condition around the flicker code:

onClipEvent(enterFrame){
if(ef_canflicker){
if(_visible){
_visible = false;
}
else{
_visible = true;
}
}
}

Then, whenever you want the flicker to start, just use:

# movie clip.ef_canflicker = true;

And to stop:

# movie clip.ef_canflicker = false;
# movie clip._visible = true;

Simple, eh?

You may want the flicker to be a little slower. If so, you can use:

onclipEvent(load){
var ef_flickercount:Number = 0;
// You can't get away with not declaring this beforehand :P
var ef_canflicker:Boolean = false;
onClipEvent(enterFrame){
if(ef_canflicker){
if(ef_flickercount == 0)
if(_visible){
_visible = false;
ef_flickercount = 2;
// 2 = Number of frames for each half-flicker. Change if you want;
}
else{
_visible = true;
ef_flickercount;
}
}
ef_flickercount --;
// Decreases the variable by one each execution
}
}

If you want the effect for a game, e.g. a period of invulnerability after being attacked or gaining a power up, you may want a timer for the effect. In that case, you could use:

onClipEvent(load){
var ef_flickertime:Number = 0;
var ef_flickercount:Number = 0;
}
onClipEvent(enterFrame){
if(ef_flickertime > 0){
if(_visible){
_visible = false;
ef_flickercount = 2;
}
else{
_visible = true;
ef_flickercount = 2;
}
ef_flickercount --;
}
ef_flickertime --;
}

And have ef_flickertime = however many frames on the event which triggers the flicker. However, it's a lot cooler and shorter (and takes less variables) to use:

onClipEvent(load){
var ef_flickertime:Number = 100;
}
onClipEvent(enterFrame){
if(ef_flickertime > 0){
_visible = (ef_flickertime % 4 >= 2);
// The modulo (%) operator returns the remainder of left value divided by right value. So, in this case, it will return each frame 1, 0, 3, 2, 1, 0, 3, 2... With the greater than or equal to operator, this becomes false, false, true, true, false, false, true, true... and sets the _visible property accordingly.
}
ef_flickertime --;
}

To heighten the speed of the flicker here you can lower the number used by the modulo. If you want a constant flicker, make sure that the >= is half of the modulo value, and also that the value is even. You can make an unbalanced flicker where more time is spent either _visible or invisible by making the >= above or below the mid point here.

That's about it for various ways of achieving a simple _visible flicker effect via AS. You can use the same principle to alternate between brightnesses, _alpha, RGB, whatever you want really. I'm gonna go have a lie down now...

Feel free to point out any errors. I've tested pretty much all of it, but some mistakes could have crept through.

I sure hope there isn't an easier way fo doing this...


BBS Signature
citricsquid
citricsquid
  • Member since: Jun. 25, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: Flicker effect May. 7th, 2006 @ 04:45 AM Reply

Preview? And looks good :D

Vengeance
Vengeance
  • Member since: Mar. 18, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Flicker effect May. 7th, 2006 @ 04:52 AM Reply

Tldr.
i read the codes though, it's a really simple effect that can look good if used well. and it look's like you went into detail. good job.


========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature
Depredation
Depredation
  • Member since: Sep. 5, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS: Flicker effect May. 7th, 2006 @ 05:52 AM Reply

Preview Pleasings.

Nice Tut ;)

BBS Signature
shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Flicker effect May. 7th, 2006 @ 05:59 AM Reply

lol >_< simple but still cool.
Although.. a tutorial on how this works is kinda overkill.... it is very basic... and dosnt realy need a tutorial... if the fools cant make this with out a tutorial they dont deserve to use it :P

Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: Flicker effect May. 7th, 2006 @ 06:50 AM Reply

At 5/7/06 05:59 AM, shazwoogle wrote: Although.. a tutorial on how this works is kinda overkill.... it is very basic... and dosnt realy need a tutorial... if the fools cant make this with out a tutorial they dont deserve to use it :P

I dunno, the modulo part is slightly more complex. There aren't many occasions when it's used.


BBS Signature
Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Flicker effect May. 7th, 2006 @ 06:33 PM Reply

Yeah... hell... I've never seen..

# movie clip.ef_canflicker = false;

Before. What the hell does the Modulo do? I know this statement is setting ef_canflicker to false, but where does that live? Why reference the movie clip? Is this like turning a function on and off?

Explain.


Perpetually looking for time to return to the arts.

BBS Signature