HTML5 training course – 13th & 14th December 2010 – London

Learn HTML5 before 2011!!!

Nils is running our next course on 13th & 14th December 2010.

Book now on:

http://html5-december-auto.eventbrite.com/

or training@dndigital.net

Hello: 0 2 0 7 7 4 9 3 7 7 0

What can you expect?

http://dndigital.net/training/html5/

See you soon!

RobotLegs Flex Popup Example

There has been some discussion on the RobotLegs AS3 Framework Google Group about the best workflow for launching popups in Flex, so I thought I’d do a quick sample. (more…)

Flex UIComponent subclass needs to resize images

The title could do with some improving – what I’m trying to say is that when subclassing UIComponent, any Image component instances need to be given an appropriate width/height. I have overlooked this time and time again, only to find that the image doesn’t appear – because it’s width and height are initialised to zero.

The following code will result in an image with zero width/height, so the image won’t show:

var img:Image = new Image();
img.source = "myfile.jpg";

Solutions:

  1. Assign specific values for width and height:
    img.width = 100;
    img.height = 100;
  2. Set the ‘scaleContent’ property to false – the image will then resize its container automatically, once it has loaded. Note that this technique has no effect if loading image from non-trusted domains!
    img.scaleContent = false;
     
    // or, if loading cross-domain, provided a crossdomain policy file allows access
    img.trustContent = true;
    img.scaleContent = false;
  3. Use an event listener to detect when the image has finished loading, then assign the correct width and height. Again, this will only work if the image has been loaded from a trusted domain.
    img.addEventListener(Event.COMPLETE, onImageComplete);
     
    protected function onImageComplete(evt:Event):void
    {
        var img:Image = evt.target as Image;
        img.removeEventListener(Event.COMPLETE, onImageComplete);
        img.width = img.content.width;
        img.height = img.content.height;
    }

I’ve mainly blogged about it so I remember the next time I write some code – but maybe it will also help somebody else debug their missing image problems.

Accessing flashvars in a custom Flex preloader

Here’s a little code snippet that allows you to access flashvar parameters inside a custom Flex preloader.

The custom preloader class extends Sprite and implements the IPreloaderDisplay class. Once the Sprite’s ‘root’ property becomes available, you can use the standard AS3 way of accessing flashvars, namely:

var flashvars:Object = root.loaderInfo.parameters;

This code should be used once the ‘root’ property has been initialised – for example in the ‘initialize()’ method defined by the IPreloaderDisplay interface:

public function initialize():void
{
     var flashvars:Object = root.loaderInfo.parameters;
}

Just for reference, compare the above approach with the standard Flex method of accessing flashvar parameters – which becomes possible once the main Application instance has been created:

var flashvars:Object = Application.application.parameters