29 June 2009

Flex Event Calendar

A article on How to create Flex Event Calendar. Please check following link:-

http://www.thetechlabs.com/tutorials/interfaces/create-a-dynamic-event-calendar-in-flex-builder-3-with-actionscript-30

05 May 2009

Using CSS & Font Embeing in Flash CS3

I found many developers asking for implementing CSS and embed fonts in Flash but due to lack of good examples they just go into loop of forums and no results come up. 


Anyways here is a small example written in Actionscript 3.0 which will load an external CS, font file and apply CSS on a text field in Flash. So, here you go:-
  1. Create a Class file and name it CSSFormattingExample.as.
  2. Lets copy below code and paste it in "CSSFormattingExample.as".


  3. Code:

    package
    {

         import flash.display.MovieClip;
         import flash.display.Sprite;
         import flash.events.Event;
         import flash.net.URLLoader;
         import flash.net.URLRequest;
         import flash.text.StyleSheet;
         import flash.text.TextField;
         import flash.text.TextFieldAutoSize;


        public class CSSFormattingExample extends MovieClip
         {

              private var fontLoader:URLLoader;
              private var loader:URLLoader;
              private var field:TextField;
              private var exampleText:String = "<h1>This is a headline</h1>" + "<p>This is a line of text.</p> <span class=\"bluetext\">" + "This line of text is colored blue.</span>";


             public function CSSFormattingExample():void
              {


                   fontLoader = new URLLoader();
                  fontLoader.addEventListener(Event.COMPLETE, onFontLoaded);
                  fontLoader.load(new URLRequest("fonts.swf"));

              }


              public function onFontLoaded(event:Event):void
              {


                   field = txtField;
                   field.width = 300;
                   field.autoSize = TextFieldAutoSize.LEFT;
                   field.wordWrap = true;
                   var req:URLRequest = new URLRequest("example.css");
                   loader = new URLLoader();
                   loader.addEventListener(Event.COMPLETE, onCSSFileLoaded);
                   loader.load(req);

              }


             public function onCSSFileLoaded(event:Event):void
              {
                   var sheet:StyleSheet = new StyleSheet();
                   sheet.parseCSS(loader.data);
                   field.styleSheet = sheet;
                   field.htmlText = exampleText;
              }
         }
    }



  4. Create a new CSS file including below code and lets save it as "example.css"


  5. CSS File:

    p {
    font-family: Arial Rounded MT Bold;
    font-size: 14;
    }
    h1 {
    font-family: Arial Rounded MT Bold;
    font-size: 30;
    font-weight: bold;
    }
    .bluetext {
    color: #0000CC;
    font-size: 14;
    }

  6. Open a new FLA file and save it as "CSSFormattingExample.fla"
  7. Create a new movieclip name it "CSSFormattingExample".
  8. Select "Export for ActionScript" checkbox from movie clip properties panel.
  9. In Class textbox write class name as "CSSFormattingExample".
  10. Create a new text field in "CSSFormattingExample" movieclip and write instance name of text field as "txtField".
  11. Now create another new FLA file and save it as "fonts.fla".
  12. Create a new dynamic text field and select font as "Arial Rounded MT Bold".
  13. Click Embed button from Properties panel of text field and embed desired font outlines.
  14. Publish this file.
  15. Try publish your "CSSFormattingExample" FLA and you are done.
To get a copy of the above example please click here for source files.

23 April 2009

Creating Image Gallery component in ActionScript 3.0

I have wrttten a new tutorial where your will learn how to create a Image Gallry component in Flash CS3 using ActionScript 3.0 and thereafter we will link this component with FlickR API for loading multiple images.


26 February 2009

Create a duplicate of an Object in Flash using Object.copy()

Yesterday I was encounter a problem of storing a Object in another Object and making changes to that Object in ActionScript 1.0. Now what Flash do, it will create the reference of that Object and will not create a copy of that Object so if you make any changes to any of the property of first object it will also update second object automatically. Now to overcome this problem I have created a small piece of code to make the copy of Object rather than storing the reference of the Object. I hope it will help you too:-

Code:
Object.prototype.copy = function ()
{
var prop, obj = new this.__proto__.constructor ();
for (prop in this)
{
if (typeof this[prop] == "object")
{
obj[prop] = this[prop].copy ();
}
else
{
obj[prop] = this[prop];
}
}
return obj;
};

Usage:

var myObj1:Object = new Object();
myObj1.prop1 = "myValue";

var myObj2:Object = myObj1.copy();
myObj2.prop1 = "myValue2";

trace(myObj1.prop1);
trace(myObj2.prop1);


05 February 2009

Flex, AIR and SQLite

Dear all,

Yesterday I was reading about connecting Flex with SQLite so we do not need to have any third party tool/application to connect some database. It seems me really useful to have a SQLdatabase and we could directly call it from Flex. No third party plug-ins required for this. I have managed to develop a small code which will create database, table and allow to perform normal login authentication in Flex, you can download source files from here. All you need to run is only Flex.

I hope it will help you too.

05 December 2008

XML 2 Object converter in AS 2.0

Hey mates,
Here is a small class for converting a XML to Object. It will help you track XML data in a very simple way using dot(.) notation with each node's name. I'm elaborating it below with a sample XML:-

XML File


    <root>

        <node1>Node 1 Text goes here</node1>

        <node2 id="2">Node 2 data</node2>

        <node3>

            <sub_node3>Sub Node 3</sub_node3>

        </node3>

    </root>


Flash Code

var objDataXML:XML = new XML ();

objDataXML.ignoreWhite = true;

objDataXML.onLoad = onXMLLoad;

objDataXML.load ("myXML.xml");



function onXMLLoad(success)

{

    if (success)

    {

        var m_objXMLData = new Object ();

        XML2Object.parseXML (m_objXMLData, objDataXML.firstChild);

        trace(m_objXMLData.node1.value);

        trace(m_objXMLData.node2.attributes.id);

        trace(m_objXMLData.node3.sub_node3.value);

    }

}

Download the class file(XML2Object.AS) by clicking here.

04 December 2008

Data comunication between AS3 and Server Side Script(PHP)

function onSubmit(evt:MouseEvent)

{

                var myData:URLRequest = new URLRequest("suggestion.php")

                myData.method = URLRequestMethod.POST

                var variables:URLVariables = new URLVariables()




                variables.name = "Dinesh Kumar Sinha";

                variables.email = "abc@gmail.com";

                variables.subject = "test mail";

                variables.suggestion = "it is just test mail";




                myData.data = variables       

                var loader:URLLoader = new URLLoader()

                loader.dataFormat = URLLoaderDataFormat.VARIABLES

                loader.addEventListener(Event.COMPLETE, dataOnLoad)

                loader.load(myData)

               

}


function dataOnLoad(evt:Event)

{

                txt_message.text = evt.target.data.message;


                if(evt.target.data.status == "mail_sent")

                {                              

                                trace("Mail Sent")

                }

                else

                {

                                trace("Error while sending")

                }

}

search_btn.addEventListener(MouseEvent.CLICK , onSubmit);


Note: You must not forget to return value from server side script(PHP, JSP etc.)


Example(PHP) : echo "status=mail_sent&message=Mail has been sent successfully";


 

25 November 2008

Loading multiple files in Flash using ActionScript 3(AS3)

I seems people asked so many times to how to load multiple files in ActionScript 3 or in other words create a preloader for multiple files in ActionScript 3. So guys below is the small piece of code which would help you in that. It just takes an array of files which all needs to be loaded and checks on enter frame for complete loading.


Code:
var arr:Array = new Array("1.swf", "2.swf");

var arrLoaders:Array = new Array();

var mcMain:MovieClip


function preloaderFiles():void

{

    mcMain = new MovieClip();

    for(var i=0;i<arr.length;i++)

    {

        arrLoaders[i] = new Loader();

        arrLoaders[i].load(new URLRequest(arr[i]));

        arrLoaders[i].visible = false;

        this.addChild(arrLoaders[i]);

    }

   

   

    mcMain.addEventListener(Event.ENTER_FRAME, onEnteringFrame);

}


function onEnteringFrame(event:Event):void

{

    var intTotal = 0;

    var intLoaded = 0;

    for(var i=0;i<arr.length;i++)

    {

        var ldr:Loader = arrLoaders[i];

        intTotal += ldr.contentLoaderInfo.bytesTotal;

        intLoaded += ldr.contentLoaderInfo.bytesLoaded;

       

        trace(arr[i] + " : " + ldr.contentLoaderInfo.bytesLoaded + " : " + ldr.contentLoaderInfo.bytesTotal)

       

    }

    trace(intLoaded != 0 && intTotal == intLoaded)

    if(intLoaded != 0 && intTotal == intLoaded)

    {

        mcMain.removeEventListener(Event.ENTER_FRAME, onEnteringFrame);

       

        for(var i=0;i<arr.length;i++)

        {

            var ldr:Loader = arrLoaders[i];

            ldr.visible = true;

        }

    }

}


preloaderFiles();


I'm also attaching the source code with 2 example files. Just click here to download the code in ZIP format.

21 November 2008

How to comunicate between Flex and Flash

Create a 'FlashFile.fla' in src folder of flex project with 'btnSendDataToFlex' button, 'txtOutput' textfield and following code on frame :
var nCounter:int = 1;
function onSendData(evt:Event):void {
objFlex.setData("Data from Flash...." + nCounter);
nCounter++;
}
function setData(strValue:String):void {
txtOutput.text = strValue;
}
function initialize () {
if(this.parent.parent != null) {
objFlex = this.parent.parent.parent;
} else {
objFlex = this;
}
}
btnSendDataToFlex.addEventListener(MouseEvent.CLICK, onSendData);
var objFlex:*;
initialize ();

Following is Flex' code :

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="1106" height="410">

<mx:Script>

<![CDATA[

private var nCounter:int = 1;

public function setData(strValue):void{

txtOutput.text = strValue;

}

private function sendDataToFlash():void {

var objFlash:* = flashComponent.content;

nCounter++;

objFlash.setData("Data From flex........" + nCounter);

}



]]>

</mx:Script>


<mx:SWFLoader source="FlashFile.swf" id="flashComponent"/>

<mx:Canvas x="558" y="1" width="540" height="400" borderColor="#4C9D5F" backgroundColor="#348948">

<mx:Button x="37" y="22" label="Send to Flash" id="btnSendToFlash" click="sendDataToFlash()"/>

<mx:Text x="151" y="24" text="Text" width="313" id="txtOutput"/>

</mx:Canvas>

</mx:Application>

19 November 2008

How to upgrade flex builder 3 to publish for flash player 10.

You can upgrade flex builder 3 with SDK 3.2.0.3794 to publish swf for flash player 10.


Download following files




Click here to see a tutorial for getting help on 'How to upgrade Flex SDK'
After configuring install 'FB3 Upadater.exe'