Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Archive for the ‘AIR’ Category

Converting a Vector to Array

leave a comment »

public function vectorToArray( value:* ) : Array
{
var vectorCount:int = value.length;
var newArray:Array = new Array();
for( var i:int = 0; i < vectorCount; i++ ){
newArray[i] = value[i];
}
return newArray;
}

Written by Deepanjan Das

June 13, 2010 at 7:30 AM

Garbage collection hack in ActionScript

leave a comment »

//unsupported hack that seems to force a full Garbage Collection in Flash ActionScript
public function forceGarbageCollection():void
{
try {
var lc1:LocalConnection = new LocalConnection();
var lc2:LocalConnection = new LocalConnection();

lc1.connect(‘name’);
lc2.connect(‘name’);
}catch (error:Error){
}

Skinning DateChooser in Flex

with 2 comments

Using css styles for Flex DateChooser component. Hope this will help someone out there 🙂

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
           xmlns:mp="valueObjects.*"
           width="240" height="235"
           borderStyle="solid" borderColor="0xFFFFFF" borderThickness="1"
           backgroundColor="0x000000"
           horizontalScrollPolicy="off" verticalScrollPolicy="off"
           cornerRadius="12"
           dropShadowColor="0x000000" dropShadowEnabled="true" shadowDistance="3" shadowDirection="right"
           creationComplete="onCreationComplete()"
           fontFamily="Arial" fontWeight="bold" fontSize="12"
           mouseDownOutside="{dispatchEvent(new DateEvent(DateEvent.CLOSE, null));}" >

    <mx:Style>
        .todayStyle{
            color: #E41D8B;
        }
        .dropDownStyle{
            background-color: #E1E1E1;
        }
        .dateStyle{
            color: #E41D8B;
            roll-over-color: #E41D8B;
        }
        .myComboBox{
            disabledSkin: Embed(source="assets/library/assets.swf", symbol="ComboBox_upSkin");
            downSkin: Embed(source="assets/library/assets.swf", symbol="ComboBox_upSkin");
            overSkin: Embed(source="assets/library/assets.swf", symbol="ComboBox_upSkin");
            upSkin: Embed(source="assets/library/assets.swf", symbol="ComboBox_upSkin");
        }
        .comboTextStyle{
            text-indent: 5;
            color: #E1E1E1;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            import events.DateEvent;
            import mx.managers.PopUpManager;
            import valueObjects.Utils;
            import mx.events.DateChooserEventDetail;
            import mx.events.DateChooserEvent;
            import mx.events.CalendarLayoutChangeEvent;

            private var dateObject:Date = null;

            public function set date(d:Date):void{
                dateObject = d;
                onCreationComplete();
            }

            public function get date():Date{
                return dateObject;
            }

            public function onCreationComplete():void
            {
                var currentDate : Date = new Date();
                // simple init
                this.months.selectedIndex = currentDate.getMonth();
                this.years.selectedItem = currentDate.getFullYear().toString();
                // register some event listeners
                //this.calend.addEventListener(DateChooserEvent.SCROLL, onChangeMonth);
                if(!this.months.hasEventListener(Event.CHANGE)){
                    this.months.addEventListener(Event.CHANGE, onChangeDropDownMonth);
                    this.years.addEventListener(Event.CHANGE, onChangeDropDownYears);
                }
                //
                calend.selectedDate = dateObject;
                this.months.selectedIndex = dateObject.getMonth();
                this.years.selectedItem = dateObject.getFullYear();
            }

            private function onChangeDropDownMonth( event : Event ) : void
            {
                this.calend.displayedMonth = (this.months.selectedIndex as Number);
                this.updateLabel();
            }

            private function onChangeDropDownYears( event : Event ) : void
            {
                this.calend.displayedYear = this.years.selectedItem.toString();
                this.updateLabel();
            }

            private function updateLabel() : void
            {
                //new Date(this.years.selectedItem.toString(), this.months.selectedIndex).toString();
            }

            private function onDateChange(event:Event) : void
            {
                 var dateEvent:DateEvent = new DateEvent(DateEvent.CLOSE, calend.selectedDate.toDateString());
                 dispatchEvent(dateEvent);
            }

            private function onComboOpen(event:Event) : void
            {
                 event.target.filters= [new GlowFilter(0xE41D8B, 1, 10, 10, 2, 1)];
            }

            private function onComboClose(event:Event) : void
            {
                 event.target.filters= [];
            }
        ]]>
    </mx:Script>
<mp:CustomDateChooser id="calend" name="calendar" calendarContentBackgroundAlpha="0" width="200" height="200" borderThickness="0" backgroundColor="0x000000" borderColor="0x000000" todayStyleName="todayStyle" styleName="dateStyle" todayColor="0x000000" color="0xFFFFFF" selectionColor="0xE41D8B"   headerColors="[0x000000, 0x000000]" yearNavigationEnabled="false" cornerRadius="6" fontWeight="bold" left="16" top="20" change="onDateChange(event);" />
<mx:ComboBox id="months" x="21" y="23" width="105" height="26" fillAlphas="[1.0, 1.0, 1.0, 1.0]" dropDownStyleName="dropDownStyle"    textInputStyleName="comboTextStyle" textSelectedColor="0xE1E1E1" textRollOverColor="0xE41D8B" selectionColor="0xE41D8B" rollOverColor="0xE1E1E1" color="0x666666" styleName="myComboBox" open="onComboOpen(event);" close="onComboClose(event);" >
<mx:ArrayCollection>
<mx:String>January</mx:String>
<mx:String>February</mx:String>
<mx:String>March</mx:String>
<mx:String>April</mx:String>
<mx:String>May</mx:String>
<mx:String>June</mx:String>
<mx:String>July</mx:String>
<mx:String>August</mx:String>
<mx:String>September</mx:String>
<mx:String>October</mx:String>
<mx:String>November</mx:String>
<mx:String>December</mx:String>
</mx:ArrayCollection>
</mx:ComboBox>
<mx:ComboBox id="years" x="136" y="23" width="75" height="26" fillAlphas="[1.0, 1.0, 1.0, 1.0]" dropDownStyleName="dropDownStyle"  textInputStyleName="comboTextStyle" textSelectedColor="0xE1E1E1" textRollOverColor="0xE41D8B" selectionColor="0xE41D8B" rollOverColor="0xE1E1E1"  color="0x666666" styleName="myComboBox" open="onComboOpen(event);" close="onComboClose(event);" >
<mx:ArrayCollection>
<mx:String>1990</mx:String>
<mx:String>1991</mx:String>
<mx:String>1992</mx:String>
<mx:String>1993</mx:String>
<mx:String>1994</mx:String>
<mx:String>1995</mx:String>
<mx:String>1996</mx:String>
<mx:String>1997</mx:String>
<mx:String>1998</mx:String>
<mx:String>1999</mx:String>
<mx:String>2000</mx:String>
<mx:String>2001</mx:String>
<mx:String>2002</mx:String>
<mx:String>2003</mx:String>
<mx:String>2004</mx:String>
<mx:String>2005</mx:String>
<mx:String>2006</mx:String>
<mx:String>2007</mx:String>
<mx:String>2008</mx:String>
<mx:String>2009</mx:String>
<mx:String>2010</mx:String>
<mx:String>2011</mx:String>
<mx:String>2012</mx:String>
<mx:String>2013</mx:String>
<mx:String>2014</mx:String>
<mx:String>2015</mx:String>
<mx:String>2016</mx:String>
<mx:String>2017</mx:String>
<mx:String>2018</mx:String>
<mx:String>2020</mx:String>
<mx:String>2020</mx:String>
</mx:ArrayCollection>
</mx:ComboBox>
</mx:Canvas>

//The CustomDateChooser.as class

package valueObjects
{
import mx.controls.DateChooser;

// create a new style setting
[Style(name="calendarContentBackgroundAlpha", type="Number", inherit="no")]

public class CustomDateChooser extends DateChooser
{
public function CustomDateChooser()
{
super();
}

// override the createChildren function to make the changes
override protected function createChildren():void
{
super.createChildren();
super.getChildAt(1).alpha = getStyle("calendarContentBackgroundAlpha");
super.getChildAt(0).alpha = getStyle("calendarContentBackgroundAlpha");
}
}
}

Written by Deepanjan Das

June 5, 2010 at 10:08 PM

SQLite Database Administration Tool

leave a comment »

I have been working with SQLite + Adobe AIR now for quite some time.
I have been using Lita (created by David Deraedt) for viewing the database and quick operations outside the scope of my applications.

It includes a visual query builder, an SQL editor with syntax highlighting and code completion, visual table and view designers and powerful import and export capabilities.

Supported platforms: Windows 2000, XP, Vista, 7.

Overall SQLite Admin is good, and more importantly it’s free!

Download it here.

Written by Deepanjan Das

June 4, 2010 at 12:40 PM

Posted in AIR, SQLite

Tagged with , ,

Tips & Tricks for improving AS3.0 performance

with 2 comments

Here are some filtered practices and techniques that can be adopted into flash/flex application development process:

1. Avoid the new operator when creating Arrays
use –
var a = [];
instead of – var a = new Array();
Same for creating Objects.

2. Fastest way to copy an array:
var copy : Array = sourceArray.concat();

3. Use static for properties methods that do not require an object instance
StringUtils.trim( “text with space at end ” );
Class definition:

package{
public final class StringUtils
{

public static function trim( s : String ) : String
{
var trimmed : String;
// implementation
return trimmed;

}}}

4. Use const for properties that will never change throughout the lifecycle of the application
public const APPLICATION_PUBLISHER : String = “Company, Inc.”;

5. Use final when no subclasses need to be created of a class
public final class StringUtils

6. Length of method/variable names doesn’t matter in AS3 (true in other langs)
someVeryLongMethodNameDoesntReallyImpactPerformanceMuch();

7. One line assignments DO NOT buy any performance (true in other langs)
var i=0; j=10; k=200;

8. No difference in memory usage between an if statement and a switch statement
if ( condition ){

// handle condition

}

IDENTICAL MEMORY USAGE:

switch ( condition )
{

case “A”:
// logic to handle case A
break;

case “B”:
// logic to handle case B
break;

}

9. Rank your if statements in order of comparisons most likely to be true

if ( conditionThatHappensAlot ) {

// logic to handle frequently met condition

} else if ( conditionThatHappensSomtimes ) {

// handle the case that happens occaisonally

} else {

// handle the case that doesn’t happen that often

}

10. AVM promotes int to Number during calculations inside loops

11. Use integers for iterations
use –
(var i: int = 0; i < n; i++)
instead of – (var i: Number = 0; i < n; i++)

12. When dealing with indices, use the value -1 to mean “no index”.

13. Locally store function values in for and while statements instead of repeatedly accessing them
var toRadians:Number = a*180/Math.PI;
for (..){ b* toRadians; }

14. Frame Rate
Despite all discussions otherwise, there is no magic framerate. I use 25 or 30 because (as far as I know) I like it best. At some point I tested and determined one was slightly better than the other, but generally speaking this is not going to be the primary cause of a site running slow. I wouldn’t generally advise going higher than 30 though, just because you’re asking the player to render an awful lot awfully fast…

15. Use ENTER_FRAME events instead of Timer events where ever possible.

16. visible is better than alpha
alpha = 0 and visible = false are totally different things. Alpha determines the opacity of a clip. Visible determines whether or not the clip should actually be rendered by the player. If you’re setting something all the way invisible, use the visible property.

17. Use try catch where ever possible
Each try, catch, and finally (optional) represent 3 different types of code blocks that can be used within a try..catch..finally statement.

18. Multiply vs. Divide:
use –
5000*0.001
instead of – 5000/1000

19. Use RegEx for validation, use string methods for searching.

20. Reuse objects to maintain a “memory plateau” DisplayObjects, URLLoader objects

21. Follow the Flex component model for custom components for better performance:
createChildren();
commitProperties();
updateDisplayList();

22. Avoid the setStyle() method (One of the most expensive calls in the Flex framework)

23. Try to avoid Repeaters for scrollable data

24. cacheAsBitmap and BitmapData
Where possible use cacheAsBitmap to rasterize vector items. You’ll force Flash to draw the symbol one time and then never again. On the flip side, if you’re scaling or rotating a symbol NEVER set it to cacheAsBitmap. Then you force Flash to render AND recapture the raster every frame, making the process slower instead of faster.

25. Use Vectors for fixed data typed Arrays


Written by Deepanjan Das

May 27, 2010 at 2:48 PM