Author Topic: FlxButton Animations (para 2.55)  (Read 2930 times)

cltatman

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
FlxButton Animations (para 2.55)
« on: Thu, Oct 27, 2011 »
So I was setting up a pretty heavily customized FlxButton and ended up getting a little abusive.  :'(

Code: [Select]
    public function Portrait()
    {
      super( 50,50 );

      loadGraphic( ButtonPNG, true, false, 150, 250 );

      addAnimation( 'idle', [6283] ); // None of these frame arrays do anything.
      addAnimation( 'down', [1147] ); // The button has decided 'idle' = 0,
      addAnimation( 'over', [4738] ); //  'down' = 2, 'over' = 1.
                                      // OH. TURNS OUT FLIXEL IGNORES ALL OF
                                      // THIS AND SETS FRAMES MANUALLY IN
                                      // FLXBUTTON.UPDATEBUTTON(). AWESOME.
      play( 'idle' );                 // THIS TOTALLY WASN'T A WASTE OF TIME.
                                      // GOOD THING THAT WAS IN THE DOCS. >:(
      onUp   = function():void{ FlxG.log( 'up'   ); play( 'idle' ); } // fails
      onDown = function():void{ FlxG.log( 'down' ); play( 'down' ); } // works
      onOut  = function():void{ FlxG.log( 'out'  ); play( 'idle' ); } // works
      onOver = function():void{ FlxG.log( 'over' ); play( 'over' ); } // works
    }

In my defense I'm new to AS3, I spent more than a little while scouring my code for vermin before cracking Flixel open; I was waist-deep before I realized it was a feature. Long story short, attached is the small addition of a public 'hijackAnimation' bool to FlxButton.

Code: [Select]
// hijackAnimation is true by default - standard button behavior.
myButton.hijackAnimation = false;
// You're now free to move about the cabin, custom animations and all that.
myButton.addAnimation( 'custom', [0,1,2,3], 10 );
myButton.play( 'custom' );
« Last Edit: Thu, Oct 27, 2011 by cltatman »

datorum

  • Active Member
  • ***
  • Posts: 165
  • Karma: +0/-0
    • View Profile
Re: FlxButton Animations (para 2.55)
« Reply #1 on: Thu, Nov 3, 2011 »
I think you forgot to add the code you modified... :(

ubersheep

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: FlxButton Animations (para 2.55)
« Reply #2 on: Tue, Feb 5, 2013 »
Sorry to revive this old thread, but I have had the exact same problem and lost around an hours worth of dev time because of it :(
FlxButtons have default animations of their own on frames 0, 1 and 2 that cannot be overridden by default. To allow defining custom animations for standard FlxButtons, make the following changes to FlxButton:

FlxButton.as - Add the following variable (around line 98):
Code: [Select]
/**
* Whether or not the button should use its own animation set.
*/
protected var _customAnims:Boolean;

FlxButton.as - Change the constructor to accept this additional variable (line 113):
Code: [Select]
from
 public function FlxButton(X:Number=0,Y:Number=0,Label:String=null,OnClick:Function=null)
to
 public function FlxButton(X:Number=0,Y:Number=0,Label:String=null,OnClick:Function=null,CustomAnim:Boolean=false)

FlxButton.as - Accept this variable in the constructor (line 139):
Code: [Select]
_customAnims = CustomAnim;

FlxButton.as - and at the bottom of protected function updateButton():void, wrap the last line in if ( !_customAnims ) (line 281):
Code: [Select]
//Then pick the appropriate frame of animation
if ( !_customAnims )
{
if((status == HIGHLIGHT) && _onToggle)
frame = NORMAL;
else
frame = status;
}

I assume this is what datorum was about to post. Apologies for the thread rebirth, but this thread was the first result for 'flixel button animations' so I imagine many have been hitting the same issue.