Author Topic: Setting Button Width  (Read 1639 times)

GrimPanda

  • Member
  • **
  • Posts: 46
  • Karma: +1/-0
    • View Profile
    • Grim Panda Games
Setting Button Width
« on: Fri, Jul 8, 2011 »
I've been away from Flixel for several releases, so perhaps this is just something that I just can't find in the docs.

I'm trying to create a button.  This button will get it's text from an external data source, and it's length will be unknown to me at run time.  It's basically a user name, and is formatted into the correct font, size, color etc as an FlxText object.  I then get the width of that string in pixels through a custom function.  Everything is great so far.

Now I need a button to place this FlxText on and set the width of that button to the width of the string in the FlxText object. (plus padding etc).  So I drop a new button down, set the label to my FlxText Object, and then theoritically, I would set the width of the button as needed.

Code: [Select]
_myBtn = new FlxButton(100, 100, '', goDoStuff);
_myBtn.label = _myTxt;
_myBtn.width = 200;  //_myTxt.pixelWidth
this.add(_myBtn);

Line 3 above silently does nothing.  It actually get's it width from my function there, but I can put in a fixed width (so we can avoid 'your function isn't returning a valid value') and it still will not set the button width. 

FlxSprite.createGraphic() is gone (I'm using 2.55), and FlxButton.loadGraphic() is looking for a class object embed, which I don't want to use since the button is a variable width at runtime.  (although I would love to use a graphic for the button that can resize itself gracefully, but that seems a bit complicated.  I was thinking of doing something like a mid section that tiles a background, and then capping the ends with the 'end images', as you would do in CSS for example.  But this seems like a headache in as3).

I know setting a button width has got to be mindnumbingly easy, but somehow I'm hitting a wall here.  Any idea how to accomplish this easily, or why FlxButton.width isn't doing anything in my code?

Thanks a ton.
"Mortis Ailuropoda"

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Setting Button Width
« Reply #1 on: Fri, Jul 8, 2011 »
The default button graphic is just an embedded graphic that comes with flixel. Take a look inside FlxButton you'll see this:

Code: [Select]
[Embed(source="data/button.png")] protected var ImgDefaultButton:Class;
I don't see any code in there to dynamically resize this graphic. Like you suggested, it would probably take some code to split it into multiple segments etc.

CreateGraphic has actually just been renamed to makeGraphic, so I guess you could use that if you're satisfied with simple rectangular buttons.

What I do is just make 2 or 3 button sizes using standard embedded graphics and then use whichever best fits the text, even if that means that sometimes I end up with a button that has a bit of extra space around the text.. no biggie.

GrimPanda

  • Member
  • **
  • Posts: 46
  • Karma: +1/-0
    • View Profile
    • Grim Panda Games
Re: Setting Button Width
« Reply #2 on: Fri, Jul 8, 2011 »
Quote
What I do is just make 2 or 3 button sizes using standard embedded graphics and then use whichever best fits the text, even if that means that sometimes I end up with a button that has a bit of extra space around the text.. no biggie.

This may just do the trick.  I'm using their Facebook first name, which can be from 2 to 30 characters long... (what a spread!), but that should give me a good range to work with for sizes.

Thanks!
"Mortis Ailuropoda"