Pages

Monday, November 21, 2011

How to make an invisible folder on Windows?

Follow the steps to make a completely invisible folder.

Step-1: Create new directory, by default that is named as 'New Folder'.
Step-2: Press F2 to rename it as blank.
Step-3: Press Alt+255.
Step-4: Press Enter.
Step-5: Right click the unnamed folder.
Step-6: Click on the 'Properties' option in the popup menu.
Step-7: Select the 'Customize' option tab in the Properties window.
Step-8: Click on the 'Change Icon...' button.
Step-9: Search and select any blank icon, then press 'OK' button.
Step-10: Press 'Apply' button to apply changes then press 'OK' button to finalize it.

Tuesday, August 16, 2011

App: Project Artificial Intelligence (P.A.I.)

This is a basic chatbot program, that has been launched to evolve itself with time. It is basically made to chat via mobile-sms. While this app uses the same core to communicate on web.

Technical details:
  • Body: the flex 4 has been used to create the user interface for front-end.
  • Brain: the php 5 has been used as server-side language for back-end.
  • Memory: the xml has been used as database.
Application - PAI:

Thursday, August 4, 2011

AS3: Basic navigation class

Here is basic code for making a navigation class. The navigation here in is based on a counter. By taking the reference of a counter the class can be extended to achieve any type of navigation.

CODE
 package   
 {  
  /**  
   * @file CNavigator.as  
   * @author Abhishek Kumar  
   */  
  public class CNavigator   
  {  
   public var _Proceed:Function;  
   private var counter:int;  
   private var minRange:int;  
   private var maxRange:int;  
   public function CNavigator(min:int, max:int)  
   {  
    trace('CNavigator -> constructor');  
    minRange = min;  
    maxRange = max;  
    counter = 0;  
   }  
   public function navLeft():void  
   {  
    trace('CNavigator -> navLeft');  
    if (counter > minRange)  
    {  
     counter--;  
     _Proceed(counter);  
    }  
   }  
   public function navRight():void  
   {  
    trace('CNavigator -> navRight');  
    if (counter < maxRange)  
    {   
     counter++;  
     _Proceed(counter);  
    }   
   }   
  }   
 }   

USAGE
 import CNavigator;

 var oNav:CNavigator = new CNavigator(0, 5);
 oNav._Proceed = trace;

 oNav.navLeft();
 oNav.navLeft();

 oNav.navRight();
 oNav.navRight();
 oNav.navRight();
 oNav.navRight();

 oNav.navLeft();
 oNav.navLeft();
 oNav.navLeft();

Wednesday, August 3, 2011

AS3: How to load child swf in parent swf at run-time

Here is a class to load single or multiple external/child swf files in the main/parent swf at run-time.

CODE
 package   
 {  
  import flash.display.Sprite;  
  import flash.display.Loader;  
  import flash.display.MovieClip;  
  import flash.net.URLRequest;  
  import flash.events.Event;  
  import flash.events.IEventDispatcher;  
  import flash.events.ProgressEvent;  
  import flash.events.SecurityErrorEvent;  
  import flash.events.HTTPStatusEvent;  
  import flash.events.IOErrorEvent;  
  /**  
   * @file CSwfLoader.as  
   * @author Abhishek Kumar  
   */  
  public class CSwfLoader   
  {  
   public var _Proceed:Function;  
   private var swfLoader:Loader;  
   private var swfRequest:URLRequest;  
   private var swfPathList:Array;  
   private var swfList:Array = [];  
   private var swfTemp:MovieClip;  
   private var swfCount:int; 
 
   public function CSwfLoader()   
   {  
    swfLoader = new Loader();  
    swfRequest = new URLRequest();     
   }  

   public function startLoading(swfPaths:Array):void   
   {  
    swfPathList = swfPaths;  
    loadSWF(swfPathList[0]);  
   }  

   private function loadSWF(path:String):void   
   {  
    initiateListeners(swfLoader.contentLoaderInfo);  
    swfRequest.url = path;  
    swfLoader.load(swfRequest);  
   }  

   private function initiateListeners (dispatcher:IEventDispatcher):void  
   {  
    trace ("CSwfLoader -> initiateListeners: ");  
    dispatcher.addEventListener (Event.COMPLETE, completeHandler);  
    dispatcher.addEventListener (Event.OPEN, openHandler);  
    dispatcher.addEventListener (ProgressEvent.PROGRESS, progressHandler);  
    dispatcher.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
    dispatcher.addEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);  
    dispatcher.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);  
   }  

   private function destroyListeners (dispatcher:IEventDispatcher):void  
   {  
    trace ("CSwfLoader -> destroyListeners: ");  
    dispatcher.removeEventListener (Event.COMPLETE, completeHandler);  
    dispatcher.removeEventListener (Event.OPEN, openHandler);  
    dispatcher.removeEventListener (ProgressEvent.PROGRESS, progressHandler);  
    dispatcher.removeEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
    dispatcher.removeEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);  
    dispatcher.removeEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);  
   }  

   private function completeHandler (event:Event):void  
   {  
    trace ("CSwfLoader -> completeHandler: " + event);  
    destroyListeners(swfLoader.contentLoaderInfo);  
    swfTemp = event.target.content as MovieClip;  
    swfTemp.customID = swfCount;  
    swfList.push(swfTemp);  
    if (swfCount < swfPathList.length - 1)  
    {   
     swfCount++;  
     initiateListeners(swfLoader.contentLoaderInfo);  
     loadSWF(swfPathList[swfCount]);  
    }  
    else  
    {   
     swfLoader.unload();  
     swfLoader = null;   
     _Proceed (swfList);   
    }  
   }   

   private function openHandler (event:Event):void   
   {   
    trace ("CSwfLoader -> openHandler: " + event);  
   }  

   private function progressHandler (event:ProgressEvent):void  
   {  
    trace ("CSwfLoader -> progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);  
   }  

   private function securityErrorHandler (event:SecurityErrorEvent):void  
   {  
    trace ("CSwfLoader -> securityErrorHandler: " + event);  
   }  

   private function httpStatusHandler (event:HTTPStatusEvent):void  
   {  
    trace ("CSwfLoader -> httpStatusHandler: " + event);  
   }  

   private function ioErrorHandler (event:IOErrorEvent):void  
   {  
    trace ("CSwfLoader -> ioErrorHandler: " + event);  
   }  
  }  
 }  


USAGE

 package   
 {  
  import flash.display.MovieClip;  
  import flash.events.Event;  
  import flash.events.MouseEvent;  
  /**  
   * @file Main.as  
   * @author Abhishek Kumar  
   */  
  public class Main extends MovieClip  
  {  
   private var _swfPathArr:Array = [  
    '../asset/swf_1.swf',  
    '../asset/swf_2.swf',  
    '../asset/swf_3.swf',  
    '../asset/swf_4.swf',  
    '../asset/swf_5.swf',  
    '../asset/swf_6.swf'  
   ];  

   private var _swfClipsArr:Array = [];
  
   public function Main()   
   {  
    var oSwfLoader:CSwfLoader = new CSwfLoader();  
    oSwfLoader._Proceed = onCompletePreloading;  
    oSwfLoader.startLoading(_swfPathArr);  
   }  

   private function onCompletePreloading(list:Array):void   
   {  
    _swfClipsArr = list;  

    btn1_mc.buttonMode = true;  
    btn2_mc.buttonMode = true;  
    btn3_mc.buttonMode = true;  
    btn4_mc.buttonMode = true;  
    btn5_mc.buttonMode = true;  
    btn6_mc.buttonMode = true;  

    btn1_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn2_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn3_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn4_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn5_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn6_mc.addEventListener(MouseEvent.CLICK, setContent);  
   }  

   private function setContent(event:MouseEvent):void   
   {  
    var _swfToAdd:MovieClip;  
    switch(event.target.name)   
    {  
     case "btn1_mc": _swfToAdd = _swfClipsArr[0] as MovieClip; break;  
     case "btn2_mc": _swfToAdd = _swfClipsArr[1] as MovieClip; break;  
     case "btn3_mc": _swfToAdd = _swfClipsArr[2] as MovieClip; break;  
     case "btn4_mc": _swfToAdd = _swfClipsArr[3] as MovieClip; break;  
     case "btn5_mc": _swfToAdd = _swfClipsArr[4] as MovieClip; break;  
     case "btn6_mc": _swfToAdd = _swfClipsArr[5] as MovieClip; break;  
    }  
    contentContainer.removeChildAt(contentContainer.numChildren-1);  
    contentContainer.addChild(_swfToAdd);
  
    //Ex: How to access internal movieclips of loaded swf  
    _swfToAdd.title_mc.visible = false;  
    trace(_swfToAdd.customID);  
   }  
  }  
 }  


AS3: How to load XML in swf at run-time ... Alternate version

We can load xml file into swf at run-time by using following class. Here is an alternate version of xml loading class that I'd presented in my previous post [AS3: How to load XML in swf at run-time].

CODE
package
{
 import flash.display.Sprite;
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 import flash.events.Event;
 import flash.events.IEventDispatcher;
 import flash.events.ProgressEvent;
 import flash.events.SecurityErrorEvent;
 import flash.events.HTTPStatusEvent;
 import flash.events.IOErrorEvent;

 /**
  * @file CXmlLoader.as
  * @author Abhishek Kumar
  */
 public class CXmlLoader extends Sprite
 {
  public var _Proceed:Function;

  public function CXmlLoader ()
  {
    trace ("CXmlLoader -> constructor");
  }

  public function retrieve (iXml:String):void
  {
    var loader:URLLoader = new URLLoader();
    configureListeners (loader);

    var request:URLRequest = new URLRequest(iXml);
    try
    {
      loader.load (request);
    }
    catch (error:Error)
    {
      trace ("CXmlLoader -> retrieve: " + error.message);
    }
  }

  private function configureListeners (dispatcher:IEventDispatcher):void
  {
    dispatcher.addEventListener (Event.COMPLETE, completeHandler);
    dispatcher.addEventListener (Event.OPEN, openHandler);
    dispatcher.addEventListener (ProgressEvent.PROGRESS, progressHandler);
    dispatcher.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    dispatcher.addEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    dispatcher.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);
  }

  private function completeHandler (event:Event):void
  {
    trace ("CXmlLoader -> completeHandler: " + event);
    _Proceed (event.target.data);
  }

  private function openHandler (event:Event):void
  {
    trace ("CXmlLoader -> openHandler: " + event);
  }

  private function progressHandler (event:ProgressEvent):void
  {
    trace ("CXmlLoader -> progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
  }

  private function securityErrorHandler (event:SecurityErrorEvent):void
  {
    trace ("CXmlLoader -> securityErrorHandler: " + event);
  }

  private function httpStatusHandler (event:HTTPStatusEvent):void
  {
    trace ("CXmlLoader -> httpStatusHandler: " + event);
  }

  private function ioErrorHandler (event:IOErrorEvent):void
  {
    trace ("CXmlLoader -> ioErrorHandler: " + event);
  }
 }
}

Tuesday, August 2, 2011

Batch File - Bulk File Remover

Have you ever faced a problem, where you need delete a particular type of files from your directory. Well I faced that while creating a zip file of a directory. The directory was downloaded from Visual Source Safe (VSS), and people who had worked with it knows that it puts an extra file that is named vssver.scc in each and every folders and sub-folders of the directory. As it was really cumbersome method to open and delete each and every folders. So I've developed a batch file for doing this.

Code:

attrib /s -r *.scc
del /s *.scc

Steps to create the batch file:
  1. Create a file with filename: del_scc.bat
  2. Open it in notepad.
  3. Copy and paste the above code in it.
  4. Save the file.
  5. Move the file to the required location, i.e., the directory from which you want to remove .scc files.
  6. Run the file by double-clicking on it.
Understand the code:
  • The first line will change the property of all the files whose extension is .scc by converting it from read-only to writable file.
  • The second line will delete all the files whose extension is .scc from the current folder and their sub-folders.
Scope of this code:
The code can be extended to include any type of files. I'd recently used to delete .fla files from the scorm course package directory.

App: Gil_TextEditor

I was working on an e-learning project for Gujarat government. The whole content of the project was in Gujarati language. Since nobody in our team had Gujarati background, so it'd become hard to understand or modify the content. The content team, those who were creating the xml files from the requirement documents, were facing lots of time & difficulty while Gujarati content creation process. So I'd developed this tool for making the content developer's life easier.

Firstly, I'd analysed the problem, which were:
  • Manual content creation for XML takes a lot of time.
  • Symbols & Gujarati-text is being manually encoded that is error prone and time taking.
  • Manual encoding requires debugging many times to ensure correctness of encoded text.

Then, I looked for the worth of doing this, which were:
  • to speed-up the XML development  and debugging 
  • to increase accuracy
  • to ease the encoding of symbols

Finally, I had to convince my supervisor that how it fits with the business initiative and target:
  • by speeding-up the process of development & delivery
  • by cutting-back debugging cycle

At last, I got approval when I showed them the following calculation:
Total time saved = manual creation timecreation time using tool = 963.6 hours

Here, is that tool which made a big difference:


LinkProject Charter - Gil_TextEditor.pdf

Modus-operandi:
  1. Copy and paste the gujrati translated text in the white box (editing area).
  2. Click on the white box once to view it's gujrati equivalent in grey box (display area).
  3. Select whole text in the white box.
  4. Click on the Process button to encode all the special characters ('<' and '>') that creates conflict in shell.
  5. Now edit the portion as required:
    • To change the portion in english - Select the portion of text and click on English button.
    • To change the portion in symbol - Select the portion of text and click on Symbol button.
    • To change the portion in sub/superscript - Select the portion of text and click on Sub/Super button repectively.
    • To change the portion into bold/italic/underline - Select the portion of text and click on Bold/Italic/Underline button respectively.
Resources:

The 5 fonts embedded in this tool are given below:
  1. ArialMT for English characters
  2. LMG-Arun for Gujarati characters
  3. Symbol for symbols
  4. GG Subscript for subscripts
  5. GG Superscript for superscripts

Monday, August 1, 2011

AS3: A class to control 'About ...' movieclip

It is well known fact that, each and every software or program has atleast About <software_name> dialogue box. Similarly if an application is made-up in flash, it should have an About ... dialogue-box.

I usually prefer to show it over top of the application as a message box with a faded or dark background.
To achieve this in flash, there is an About ... movieclip. It should be of size that covers the whole stage.

There are 2 movieclips inside About ... movieclip:
  1. shows message related to About ... content
  2. fades or darkens the are behind About ... content

By default this About ... movieclip will remain hidden. And on certain predefined mouse-click it'll show up. But after that as soon as user will click on the dark background area, the About ... will disappear.

So this functionality requires only on movieclip inside the About ... movieclip to be linked with mouse-click event, which is the bas or background movieclip.

CODE
 package   
 {  
  import flash.display.MovieClip;   
  import flash.events.Event;  
  import flash.events.MouseEvent;  
  /**  
   * @file CAbout.as  
   * @author Abhishek Kumar  
   */  
  public class CAbout  
  {  
   private var Container:MovieClip = null;  
   public function CAbout(iReference:MovieClip)  
   {  
    Container = iReference;  
    initializeAssets();  
   }  
   private function initializeAssets():void  
   {  
    hide();  
   }  
   public function initializeEvents():void  
   {  
    Container.base.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
   }  
   public function destroyEvents():void  
   {  
    Container.base.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
   }  
   private function onMouseUp(me:MouseEvent):void  
   {  
    hide();  
    destroyEvents();  
   }  
   public function hide():void  
   {  
    Container.visible = false;  
   }  
   public function show():void  
   {  
    Container.visible = true;  
    initializeEvents();  
   }  
  }  
 }  

AS3: A class to control button behavior & states

Here is a CButtonControl.as class that'll control the behavior of a movieclip (made in flash) and make it work like a button.

The movieclip should have atleast 4 frames with labels as:
  1. up
  2. over
  3. down
  4. disable

These labelled frames will work as different states of the button.

CODE
 package   
 {  
  import flash.display.MovieClip;   
  import flash.events.Event;  
  import flash.events.MouseEvent;  
  /**  
   * @file CButtonControl.as  
   * @author Abhishek Kumar  
   */  
  public class CButtonControl  
  {  
   private var Container:MovieClip = null;  
   public var proceed:Function;  
   private var objekt:Object = {};  
   public function CButtonControl(iReference:MovieClip)   
   {  
    Container = iReference;  
    initializeAssets();  
   }  
   private function initializeAssets():void  
   {  
    objekt.id = -1;  
    Container.visible = true;  
    Container.mouseChildren = false;  
    Container.gotoAndStop('disable');  
   }  
   public function initializeEvents():void  
   {  
    Container.buttonMode = true;  
    Container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);  
    Container.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
    Container.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);  
    Container.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);  
   }  
   public function destroyEvents():void  
   {  
    Container.buttonMode = false;  
    Container.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);  
    Container.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
    Container.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);  
    Container.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);  
    //Container.removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);  
   }  
   private function onMouseDown(me:MouseEvent):void  
   {  
    Container.gotoAndStop('down');  
   }  
   private function onMouseUp(me:MouseEvent):void  
   {  
    Container.gotoAndStop('up');  
    (objekt.id == -1)? proceed() : proceed(objekt);  
   }  
   private function onMouseOver(me:MouseEvent):void  
   {  
    Container.gotoAndStop('over');  
   }  
   private function onMouseOut(me:MouseEvent):void  
   {  
    Container.gotoAndStop('up');  
   }  
   public function setLabel(iLabel:String, isHtml:Boolean=false):void  
   {  
    if(isHtml)  
     Container.caption.htmlText = iLabel;  
    else  
     Container.caption.text = iLabel;  
   }  
   public function embedFonts(bool:Boolean):void  
   {  
    Container.caption.embedFonts = bool;  
   }  
   public function hide():void  
   {  
    Container.visible = false;  
   }  
   public function show():void  
   {  
    Container.visible = true;  
   }  
   public function enable():void  
   {  
    initializeEvents();  
    Container.gotoAndStop('up');  
   }  
   public function disable():void  
   {  
    destroyEvents();  
    Container.gotoAndStop('disable');  
   }  
   public function set id(id:int):void  
   {  
    objekt.id = id;  
   }  
   public function get id():int  
   {  
    return objekt.id;  
   }  
  }  
 }  

Thursday, July 28, 2011

AS3: Transmute namespace instead of removing it in XML

There are lot many articles and solutions available on web related to the xml namespace. Few guide us over how to handle namespace in xml via as3 while rest have provided solution by removing xmlns tag from the xml.

But I think, there is better way than removing or filtering the namespace. The solution that I use is very clever one. You might have noticed that to put namespace in xml colon character is used. So keeping this property in mind, I have developed a function to replace all the colons from namespaces only with the underscore character.

The code uses regular expression to search the colon character in only namespace. And then replace it with the underscore character.

This way all the namespaces are still available and accessible but in the form of attributes. And this is the advantage of converting it into a attributes.

 /**  
  * @method transmuter  
  * @author Abhishek Kumar  
  */  
 private function transmuter(data:String):String  
 {  
  var modified:String = data.replace("xmlns=", "xmlns:default=");  
  var pattern:RegExp = new RegExp(":(?!/)", "gi");  
  var transmuted:String = modified.replace(pattern, "_");  
  return transmuted;  
 }  

The function takes xml as a string. So make the xml into string as, xmldata.toString(); then use the function to transmute it. And convert the returned xmldata that is in string format into the xml class format.

Saturday, July 2, 2011

Batch File - Bulk Renamer ... Advanced

This post is the continuation of my last post, i.e., Batch File - Bulk Renamer; related to systematically renaming all the files in a folder.

The problem I noticed with my previous code is that, either it works with the particular type of file (aka same extension) or in absence of file-extensions it renames this batch file also. So I thought to introduce a condition in this so as to skip this batch file while renaming other files.

Here is the code for that,

.:C.O.D.E:.

@echo off&set /a cnt=1
for %%a in (*) do call :PROCESS "%%a"
goto :EOF
:PROCESS
IF not %1=="bulk_renamer.bat" (rename %1 "IMG_%cnt%.jpg")
set /a cnt+=1


Note: This code would rename all the files present in the folder except bulk_renamer.bat

Help: Refer to my previous post for the steps to create or modify the script.

Link: http://akzcool.blogspot.com/2010/11/batch-file-bulk-renamer.html

Friday, July 1, 2011

AS3: To determine the angle between two given coordinates

Here is a sample code to determine the angle between two given coordinates. In other words, to calculate the angle of an object with respect to the origin.

This could be used to determine current angle of mouse-pointer with some pre-defined origin. In which case the movieclip attached to the mouse-pointer can always point towards it's origin.

Many examples of this logic can be seen on web. One of the popular example is; a female face with moving eyes that follows the user's mouse-pointer movement, so that the eyes always look towards the mouse-pointer.

Sample Code:
 package   
 {  
  import flash.geom.Point;  
  /**  
  * @file Connecteur.as  
  * @author Abhishek Kumar  
  */  
  public class Connecteur
  {  
   private var originCoordinate:Point;  
   private var currentCoordinate:Point;  
   private function setOriginCoordinate():void  
   {  
     originCoordinate = new Point(Source.x, Source.y);  
   }  
   private function setCurrentCoordinate():void  
   {  
     currentCoordinate = new Point(Pointer.x, Pointer.y);  
   }  
   private function getCurrentAngle():Number  
   {  
     var perpendicular:Number = (currentCoordinate.y - originCoordinate.y);  
     var base:Number = (currentCoordinate.x - originCoordinate.x);  
     var theta:Number = Math.atan2(perpendicular, base); // in radians  
     return (theta * 180 / Math.PI); // in degrees  
   }  
 }  

TTFEdit - TrueTypes font editor


TTFEdit is a GPL editor for TrueType fonts. TrueType is a standard used by modern operating systems like Mac OS X, Windows XP and Linux. There are two different TrueType specifications: from Apple (original author of this technology) and from Microsoft. Mac OS X and Windows XP use own font rendering engines, there is also FreeType library used by Linux. On these three systems the same font will generally look different. TTFEdit tries to produce TTF files compatible with all mentioned systems.

Download link:
http://sourceforge.net/projects/ttfedit/files/ttfedit-1.0a/1.0/ttfedit-1.0a-bin.zip/download

Hosted at SourceForge:
http://sourceforge.net/projects/ttfedit/

Tuesday, June 21, 2011

App: Date Picker


Here is sample date picking tool I'd created for my client.



App: Virtual Keyboard


Here is a sample virtual keyboard that I'd developed for one of my client.



App: Digit-To-Word Converter


The application digit-to-word converter would help in memorizing long digits number.



App: Feng-Shui Astrology


Project: Feng-Shui Astrology
Status: In progress
Description: Calculates Feng-Shui numbers



App: Which Baby Are You?


To find out which baby are you, just select the month you were born in and read the prediction given below.



App: Relationship Analysis


This application will analyse your relationship on the basis of the rules of numerology.



App: Capture Image via Webcam

This is an online image capturing tool that I'd created originally for my father when he was facing problem while capturing snaps from in-built webcam of his new laptop. He asked me to tell him the correct steps to do so. Since I was describing the steps over phone, it didn't worked. So I thought to make something versatile & easy-to-use tool for him and finally this tool got developed.

Here is that image capturing webapp tool:



Note: You'll be asked for the webcam access via Adobe Flash Player Setting popup. Here you need to click 'Allow' button. It's screen-shot is given below:

Friday, February 11, 2011

AS3: Minimum code required to load XML at run-time

Minimum code required to load XML at run-time is given below:

SOURCE CODE
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener (Event.COMPLETE, onEventComplete);
urlLoader.load (new URLRequest('Database.xml'));

function onEventComplete (e:Event):void
{
  var Xml:XML = new XML(URLLoader(e.target).data);
  trace (Xml);
}


AS3: How to dynamically load image in movieclip

Here is a minimum code required to load an image in movieclip.

SOURCE CODE
var loader:Loader = new Loader();
loader.load(new URLRequest('img_01.jpg'));
mc.addChild(loader);
Note: mc is a movieclip instance present on stage.

AS3: How to load XML in swf at run-time

We can load xml file into swf at run-time by using following class.

SOURCE CODE
 package   
 {  
  import flash.display.Sprite;  
  import flash.events.*;  
  import flash.net.*;  
  /**  
  * @file CLoader.as  
  * @author Abhishek Kumar  
  */  
  public class CLoader extends Sprite   
  {  
   private var oDispatcher:CDispatcher;  
   public function CLoader()  
   {  
    oDispatcher = CDispatcher.getInstance();  
   }  
   public function retrieve(iXml:String):void  
   {  
    var loader:URLLoader = new URLLoader();  
    configureListeners(loader);  
    var request:URLRequest = new URLRequest(iXml);  
    try   
    {  
     loader.load(request);  
    }   
    catch (error:Error)   
    {  
     trace("Loading failed!");  
    }  
   }  
   private function configureListeners(dispatcher:IEventDispatcher):void   
   {  
    dispatcher.addEventListener(Event.COMPLETE, completeHandler);  
    dispatcher.addEventListener(Event.OPEN, openHandler);  
    dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);  
    dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
    dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);  
    dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);  
   }  
   private function completeHandler(event:Event):void   
   {  
    trace("CLoader -> completeHandler: " + event);  
    var loader:URLLoader = URLLoader(event.target);  
    var Xml:XML = new XML(loader.data);  
    oDispatcher.dispatch(CDispatcher.ACTION, Xml);  
   }  
   private function openHandler(event:Event):void   
   {  
    trace("CLoader -> openHandler: " + event);  
   }  
   private function progressHandler(event:ProgressEvent):void   
   {  
    trace("CLoader -> progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);  
   }  
   private function securityErrorHandler(event:SecurityErrorEvent):void   
   {  
    trace("CLoader -> securityErrorHandler: " + event);  
   }  
   private function httpStatusHandler(event:HTTPStatusEvent):void   
   {  
    trace("CLoader -> httpStatusHandler: " + event);  
   }  
   private function ioErrorHandler(event:IOErrorEvent):void   
   {  
    trace("CLoader -> ioErrorHandler: " + event);  
   }  
  }  
 }  

USAGE
import CLoader;
var oLoader:CLoader = new CLoader();
oLoader.retrieve ('Database.xml');

XML (sample)
<?xml version="1.0" encoding="utf-8" ?>
<data>
  <item index="1" image_path="img_1.jpg" thumbnail_path="thumb_1.jpg" /> 
  <item index="2" image_path="img_2.jpg" thumbnail_path="thumb_2.jpg" />
  <item index="3" image_path="img_3.jpg" thumbnail_path="thumb_3.jpg" /> 
  <item index="4" image_path="img_4.jpg" thumbnail_path="thumb_4.jpg" /> 
  <item index="5" image_path="img_5.jpg" thumbnail_path="thumb_5.jpg" /> 
  <item index="6" image_path="img_6.jpg" thumbnail_path="thumb_6.jpg" /> 
  <item index="7" image_path="img_7.jpg" thumbnail_path="thumb_7.jpg" /> 
  <item index="8" image_path="img_8.jpg" thumbnail_path="thumb_8.jpg" /> 
  <item index="9" image_path="img_9.jpg" thumbnail_path="thumb_9.jpg" />
</data>

AS3: How to create custom event dispatcher class

We can create custom event dispatcher class as given below:

SOURCE CODE
package 
{
  import flash.events.EventDispatcher;
  import flash.events.Event;

  /**
   * @file CDispatcher.as
   * @author Abhishek Kumar
   */
  public class CDispatcher extends EventDispatcher
  {
    public static var ACTION:String = "action";
    public static var ERROR:String = "error";

    private static var instance:CDispatcher = new CDispatcher();

    public function CDispatcher() 
    {
      if (instance) 
        throw new Error("Error: Instantiation failed!");
    }

    public static function getInstance():CDispatcher 
    {
      return instance;
    }

    public function dispatch(e:String, o:Object=null):void 
    {
      dispatchEvent(new CEvent(e, o));
    }
  }
}

USAGE
import CDispatcher;
import CEvent;

var oDispatcher:CDispatcher = CDispatcher.getInstance();
oDispatcher.addEventListener (CDispatcher.ACTION, actionHandler);
oDispatcher.addEventListener (CDispatcher.ERROR, errorHandler);

function actionHandler (e:CEvent):void
{
  trace ("actionHandler: " + e);
}

function errorHandler (e:CEvent):void
{
  trace ("errorHandler: " + e.data);
}

/* 
* Anywhere inside custom made class
*/

private var oDispatcher:CDispatcher;
oDispatcher = CDispatcher.getInstance();
oDispatcher.dispatch(CDispatcher.ACTION);
oDispatcher.dispatch(CDispatcher.ERROR, 'Error#007');

AS3: How to create custom event class with extra parameters

We can create a custom event class with extra properties to pass data with it. The source code is given below.

SOURCE CODE
package 
{
  import flash.events.Event;

  /**
  * @file CEvent.as
  * @author Abhishek Kumar
  */
  public class CEvent extends Event
  {
    public static const CUSTOM_EVENT: String = "custom_event";

    public var data:Object;

    public function CEvent(type:String, data:Object, bubbles:Boolean=false, cancelable:Boolean=false)
    {
      super(type, bubbles, cancelable);
      this.data = data;
    }

    override public function clone():Event
    {
      return new CEvent(type, data, bubbles, cancelable);
    } 
  }
}
--
Note: Here we can dispatch event as show below:
dispatchEvent(new CEvent(CEvent.CUSTOM_EVENT, objekt));

AS3: How to pass extra parameter in listener function

Trick for ActionScript-3 to pass extra parameter in the listener function.

SOURCE CODE
mc.addEventListener(MouseEvent.CLICK, foo("Wow!"));

function foo(str:String)
{
   return function(e:MouseEvent) { trace(str); }
}
OUTPUT
Wow!