Archive for April, 2012

Adding Glow Filter to Component

April 26, 2012

I got one requirement where I will draw one custom component and it should glow when we mouse hover it and remove glow when move the mouse out. This is a simple thing, though I am putting this so that you can save time. 🙂

First we need to write a function which will be triggered on both mouse hover and mouse out of the component. Obviously it will accept one MouseEvent. Inside that we need to create one GlowFilter and add this filter to the component. Set the properties for the flow filter such as color, alpha and quality. On mouse out am removing all the filters. This will work for me since I do not have any other filters for the component. If you have other filters, then iterate through the filters and remove only the glow filter. The function will look like below :

private function highLightCircle(event:MouseEvent):void{
            var glowFilter: GlowFilter = new GlowFilter();
            var circ:Object = event.currentTarget;
            if(circ.filters.length == 0){  
                glowFilter.color = 0xFFFFFF;
                glowFilter.alpha = 1;
                glowFilter.blurX = 10;
                glowFilter.blurY = 10;
                glowFilter.quality = BitmapFilterQuality.HIGH;
                circ.filters = [glowFilter];
            }else{
                circ.filters = null;
    }
}

Now we need to call this on mouse over and mouse out. Just add the event listeners to the component object on which you need to apply the filter like below.

circle.addEventListener(MouseEvent.ROLL_OVER, highLightCircle);
circle.addEventListener(MouseEvent.ROLL_OUT, highLightCircle);

Thats all guys. Flex your life. Happy Coding. 🙂

Check if an XML node or attribute exists in E4X format

April 25, 2012

E4X is the heaven when it comes to XML parsing. Since flex natively supports E4X format results, it makes our lives very easy. But there are lots of tips and tricks to play in E4X parsing.

Suppose you have an optional XML node and you need to check whether it exists in the XML. Obviously our actionscript and javascript memories will ask us to try this way if(xml.node != null) or if(xml.node). But if you try this, it is not gonna work. It will probably give you unwanted result or if your day is going too good you will get a reference error. The below are some ways you can try out :

For checking Nodes :

if(xml.someNode.length() > 0){ // Node exists }if(xml.someNode != undefined)
{ // Node exists }

For checking Attributes:

if(xml.@someAttr.length() > 0)
{ // Attribute exists }

if(xml.@someAttr != undefined)
{ // Attribute exists }

That’s it guys. Happy Coding! 🙂 Flex your life.