Archive for April, 2008

Embedding fonts in Flex Builder 2 Actionscript projects

Sunday, April 27th, 2008

After spending about 2 hours searching around the ‘net for a way to do this, all I could find were tips for embedding fonts in FB3, which is should be easy enough:

[Embed(source="/Library/Fonts/Arial", fontFamily="foo", mimeType="application/x-font")] public var bar:String;

This is because one of the things that has been added to FB3 (as far as I can tell) is a font manager system. However, I did run across a load of pages with bugs in this method (and you can only use truetype fonts) and it doesn’t help me with FB2.

Eventually I found a page that suggested that I could use fonts embedded in a swf. Embedding fonts from SWF files The relevant Embed tags for actionscript projects are about 3/4 the way down. This works in both FB2 and 3.

Essentially all you do is to drop a text field onto the stage, format it as normal and then publish. In FB put something like this (it doesn’t actually matter where – the embedded font is available throughout your app):

[Embed(source="/assets/swf/arialFontAsset.swf", fontName="Arial")]
var arialPlain:String;
[Embed(source="/assets/swf/arialFontAsset.swf", fontName="ArialBold" fontWeight="bold")]
var arialBold:String;

and Bob’s your embedded font uncle!

AS2 Sliders

Friday, April 25th, 2008

There comes a time in every man’s life when he has to build sliders, alternatively, he can search the web and pluck them off some helpful bloggers site.

They come in 4 flavours:
SliderBase, which is just that – it has no functionality of its own, but provides the business of grabbing a thumb and sliding it around.

ValueSlider extends SliderBase and adds the ability to have lo and hi values on the slider, get and set the value of the slider and to add a target and properties to be changed whenever the slider is moved. Also adds snap and useFloat functionality.

InertiaSlider extends ValueSlider. This adds inertia to the slider so you can grab and throw it. You can set the friction and bounce (how it reacts at the end of the slider) there is also a ‘loop’ property that makes the slider jump to the other end of the track if it reaches the end. This is useful if you want to use the slider for 360 rotations.

Lastly, ScrollBar extends InertiaSlider. This adds the ability to scroll either text fields or movieclips and have a resizable thumb.

I’m not going to argue that these are the best sliders ever, but they do work :) They have a few quirks, one being that the slider should be set up horizontally then rotated if you want vertical sliding. They’re also pretty fussy about how you have your clips setup.

I could make them better, but they’re as2 and I really can’t be bothered. Anyway, I hope someone gets some use out of ‘em.

Download AS2 Sliders

casting boolean values from XML

Saturday, April 12th, 2008

This one is kind of logical when you think about it, but it tripped me up for a bit.
When loading Boolean values from XML, you’d think you could do this to get the value:

var myBoolean:Boolean = myXML..myBoolean;

or even explicitly cast like this:

var myBoolean:Boolean = Boolean(myXML..myBoolean);

but this will give you an erroneous reading. What you need to do is:

var myBoolean:Boolean = Boolean(String(myXML..myBoolean));

What is going on here??
Well, when Actionscript does the cast in the first 2 examples, it actually just checks whether the node exists. So if the node exists, you will get true, if it doesn’t you’ll get false. By casting to a String first it means that the cast to Boolean is forced to evaluate the node value, giving you what you expect.

[update] After some further testing, after seeing the comment below, it seems that all 3 versions give you true, no matter what. In fact, (as far as I can tell) it is impossible to correctly cast a Boolean value held in an XML node as true/false to a Boolean variable correctly.

The solution is to either use a ‘0′ or ‘1′ and cast first to a number:

var myBoolean:Boolean = Number(myXML..myBoolean);

or to do a check like:

if (myXML..myBoolean == "true") myBoolean = true
else myBoolean = false;

Repeat: I must not blog at 1am