views:

995

answers:

3

I want to make a button flash red in response to an event, and then stop flashing whenever the button is pressed. This would be much like the master alarm button in an aircraft. So the flashing effect needs to be something that I can programmatically stop.

I saw one example using a timer, but I want to avoid using a timer because I don't like to use timers unless I really have to. It just seems really chaotic to have all this timers going just to do trivial little things.

My hope is that there is some way to do a little animation with flash or something, on the button, and just tell the animation to play to make the button flash, and then stop/reset the animation to put it back at the original not flashing state.

I am using Flex Builder 3. I am new to Action Script and Flex/Flash.

Can someone give information on how to do this or point me to some information?

Thanks.

+2  A: 

I would try with a tweening engine... something like this:

function flash() {
   TweenLite.to(button, .5, {tint:0xFF0000, onComplete:unflash});
}
function unflash() {
   TweenLite.to(button, .3, {removeTint:true, onComplete:flash});
}

function deleteFlashing(e) {
   TweenLite.killTweensOf(button);
   TweenLite.to(button, .2, {removeTint:true});
}
Cay
That uses a timer, also. Just because you don't declare it yourself, it doesn't mean the TweenLite doesn't declare it for you.
evilpenguin
This may use timers, but IIRC there is only *one* global timer for all TweenLite animations going on. This is the most sane way to do programmatic using Actionscript and especially Flex (where there is no access to timelines).
James Fassett
A: 

From what I understand, you want to use a keyframe animation. That's a really simple to do. Here's some tutorials on that: http://www.mystery-productions.com/flash/keyframes.html http://www.awdsf.com/courseware/flash/flash2_keyframes.htm http://www.dummies.com/how-to/content/animating-with-flash-keyframes.html

evilpenguin
He says that he is using Flex Builder and not Flash CS3/CS4 so keyframe based animations don't seem to be appropriate.
James Fassett
<quote>My hope is that there is some way to do a little animation with flash or something</quote>Seamed a reasonable solution to the previous phrase...
evilpenguin
+1  A: 

As Cay points out - when it comes to programmatic animation you really want to use a Tween library. Fact is, someone has probably already written a more efficient solution than you can - and will give it to you for free.

There are many, many options. The maintainer of TweenLite has a speed comparison of ActionScript 3 tween engines on his website.

  • Tweener is a very popular library but it has been superseded in performance and size by other libraries recently.
  • TweenLite is a small and fast library that is gaining in popularity. The developer also maintains TweenMax which is a more featureful tweener.
  • Tweensy another lib claiming to be really fast
  • gTween is another tweener from AS3 guru Grant Skinner.

The syntax to do what you want will depend on the library, but pulsing a button red will be trivial in each one. I suggest you become familiar with one or more tween libraries for doing this sort of thing -- it will save you a huge amount of time in the long run.

James Fassett