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()
{
super();
}

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;
invalidate(InvalidationType.SIZE);
}

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);
}

super.drawLayout();

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

public function getRealWidth() : Number
{
return _width;
}

}
}

Statistics by Countries

Any excuse for drawing flags.

Click to launch application

Click to launch application

Sources :

Wikipedia, List of Countries by GDP (nominal)
Forbes, World’s Fattest Countries
Wikipedia, List of Countries by literacy rate
Wikipedia, FIFA World Rankings
Wikipedia, List of countries by population
Wikipedia, List of countries by suicide rate

Self Replication v2

This is my second version of a Self Replication application.

This time the little guys are on the hunt for food and when they feed they grow in length by one unit, just like the game snake for the old mobile phones. Each ‘snake’ has a unique set of ‘DNA’ which contains the following variables :

  • Speed of Snake : The speed of the snake will decrease as it becomes longer and increase as it shrinks.
  • Starvation Time : When the snake is not eating (looking for more food) the starvation time determines how long it takes before it looses one of it’s ‘units’ and gets shorter. The starvation time is closely linked with the speed of the snake. The faster the snake, the faster it’s unit’s diminish as it uses more energy moving around. A slower snake will use less energy and thus it’s ‘Starvation time’ is considerably longer. When the snake looses it’s final unit it dies and no longer exists.
  • Colour.
  • Digestion Time : How long it takes to convert an item of food into a part of it’s body (a ‘unit’).
  • Pregnancy Time : When a snake reaches a length of 10 units (this will be moved to the DNA in V3) it becomes pregnant and the pregnancy time determines how long it takes before an egg is laid.

When an egg is laid the DNA of the parent snake is transferred to the offspring within the egg. A random number of offspring is produced from each snake within the confines of a minimum and maximum litter boundary. The child snakes are born at a length of 1 unit and immediately need to feed or risk starvation.

Hit ‘SPACE‘ in order to show when the next harvest of food is due, or force it come sooner. Alternatively you can feed an individual area by hitting the ‘Feed Small Area‘ button.