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...
