fl.controls.Button width issues…

So recently I’ve had a few problems with the component button in Flash. Essentially I wanted the button to resize automatically to fit to the text inside and was amazed to find out (after all these years) that it doesn’t have this method! Crazy.

Anyway I managed to find a class by a dude called Matt Sweetman (http://webroo.org/2009/05/11/auto-size-button-for-flash-cs-components/) which extends the Button and adds the auto-size to it. All well and good, except that it’s still a bitch trying to align them, because we don’t know the width of the buttons when we’re cycling through them in a “for” loop. The drawLayout() method isn’t fired instantly so the alignment can’t be achieved.

The solution I came up with was to use Matt Sweetman’s class and add a dispatchEvent to it once the auto-size has been performed. You will need to set up a variable to catch the amount of buttons that have finished with their drawLayout() methods. Once this has been done simply call getRealWidth() [I’ve added this as the _width property is protected and the width property isn’t accurate] on each one and then you can align them correctly. Hopefully someone will find this useful.

Here’s the amended class :

package components
import fl.controls.Button;
import fl.core.InvalidationType;
import flash.events.Event;

import flash.text.TextFieldAutoSize;

public class AutoSizeButton extends Button
// Original class written by Matt Sweetman     - http://webroo.org/
// Ammended by Dan Sanderson                 - http://pldm.co.uk

public static const BUTTON_READY : String = "Button Ready";

public function AutoSizeButton()

protected var _autoSize:Boolean = true;

* Whether auto sizing is turned on. Default is true.

public function get autoSize():Boolean
return _autoSize;

public function set autoSize(value:Boolean):void
_autoSize = value;
textField.autoSize = _autoSize ? TextFieldAutoSize.LEFT
: TextFieldAutoSize.NONE;

override protected function drawLayout():void
if (autoSize)
// Set the component width by calculating
// text & padding widths
var txtPad:Number = Number(getStyleValue("textPadding"));
_width = (textField.textWidth + 4) + (2 * txtPad);


// Firing this so that we can align the buttons.
dispatchEvent(new Event(AutoSizeButton.BUTTON_READY));

public function getRealWidth() : Number
return _width;


AS3 + IPAC + Swana JLF-H5 Joystick

Back in 2007 I saw a talk by Craig Swann at Flash on the Beach about using an array of different house hold electrical objects (switches, keys and even wind chimes) to interact with Flash through an I-PAC USB keyboard encoder. I remember at the time thinking, woah, that looks complicated. However I recently bought myself an I-PAC and to my pleasant surprise it’s incredibly easy. It also gives me an excuse to go around hardware shops and buy all sorts of crap to hook up to my Flash Applications.

Continue reading

Problems embedding multiple members of the same font-family in Actionscript 3.0

I came across a problem the other day which it turns out is quite common. Normally when I am dealing with fonts in Actionscript I simple create a new font in the library and call that via the SWC in FDT. However, it turns out that if I make a font called Didot_Regular and Didot_Bold and tick the appropriate ‘Bold’ and ‘Regular’ boxes my compiled SWF fails to differentiate between the two.

The solution is create your fonts in a Class and compile this as a separate SWC or SWF. Here’s how :

package fonts {
	import flash.display.Sprite;

	 * @author Dan
	public class MyFonts extends Sprite {
		[Embed(source="/../fonts/Didot.ttc", fontName="Didot", fontStyle="normal", fontWeight="bold", mimeType="application/x-font-truetype")]
		public static var DidotBold		:	Class;
		[Embed(source="/../fonts/Didot.ttc", fontName="Didot", fontStyle="normal", fontWeight="normal", mimeType="application/x-font-truetype")]
		public static var Didot			:	Class;

Now compile this as a SWF or as a SWC library and then use the fonts as normal :

			var tf : TextFormat = new TextFormat();
			tf.font = new MyFonts_DidotBold().fontName;
			tf.bold = true;
			tf.size = 18;
			tf.color = 0xFFFFFF;

VerifyError: Error #1033 Cpool entry 29 is wrong type.

Don’t panic! It’s easy to solve. You’re running your SWF in a Flash Player version lower than the one you compiled it in. For me I compiled it as a FP10 SWF but my version of Eclipse ran by default in FP9 and that’s the error it spat out. If you are using Eclipse/FDT/Flash Builder then :

  • Window -> Preferences
  • FDT -> Tools -> Flash
  • Change the Flash Player path to point to Flash Player 10.

That’s it. Problem solved.