blinking movieclip with timer
i am trying to make my movieclip blink, it blinks when it is hit by something, to create the effect of a game.i have a hit test when it is hitted, caught is called on the object.the caught function of the object will make it stop and start to blink based on a timer.My timer is set on new Timer(400); why does my object not blink? my conditions seem to be correct.
if (hit.hitTestObject(f.hit))
f.caught();
private function blinkingHandler(evt:TimerEvent):void
{
_canBlink = true;
if (_canBlink)
{
开发者_StackOverflow社区this.alpha = 0;
_canBlink = false;
this.alpha = 100;
trace("blinking");
}
}
public function caught() : void
{
_blinkTimer.start();
//removeEventListener(Event.ENTER_FRAME, loop);
//this.stop();
}
First, I'm going to assume that you have added the event listener to trigger the blinkingHandler
call when the Timer fires:
_blinkTimer.addEventListener(TimerEvent.TIMER, blinkingHandler);
Now, blinkingHandler
as you have posted it will never hide the object. The alpha is set to 0, but you immediately set it back to 1 in the same call, so the net result is that the alpha does not display at 0. You must set the alpha to 0, let a few frames render, set it back to 1 on the next timer tick, and so on. Try this:
private function blinkingHandler(evt:TimerEvent):void
{
if(_canBlink) this.alpha = 1;
else this.alpha = 0;
_canBlink = !_canBlink;
}
You could even do:
visible = !visible;
To simply toggle the visibility on each timer tick.
var timer:Timer = new Timer(200);
var blink:Boolean = true;
timer.start();
sq_mc.addEventListener(MouseEvent.MOUSE_OVER, onMseOvrAction);
sq_mc.addEventListener(MouseEvent.MOUSE_OUT, onMseOutAction);
function onMseOvrAction(e:MouseEvent):void{
timer.addEventListener(TimerEvent.TIMER, timerAction);
}
function onMseOutAction(e:MouseEvent):void{
timer.removeEventListener(TimerEvent.TIMER, timerAction);
sq_mc.alpha = 1;
}
function timerAction(e:TimerEvent):void
{
if (!blink){
sq_mc.alpha = 1;
} else{
sq_mc.alpha = 0;
}
blink = !blink;
}
Mr.Allan, I have changed.
精彩评论