App in a week recorded session
Jun 8th
App in a week recorded sessions are available at
Erasing the designer-to-developer gap: Adding interactions to your design
Connecting your design to PHP services
Replacing a selected color in an image using BitmapData
Apr 11th
Photoshop has a method to replace a selected color in an image with another color. This same functionality can be achieved in Flash Actionscript(AS3) using getPixel() and setPixel() methods of BitmapData.
First step is to loop through all the pixels in the specified bitmap
for (var j = 0; j < bitmap.height; j++) {
for (var i = 0; i < bitmap.width; i++) {
trace(bitmapData.getPixel(i,j));
}
}
The above given code traces all the pixels in the bitmap.
Next step is to find all pixels with the specified color. This can be done by introducing a checking condition like one given below.
if(bitmapData.getPixel(i,j)==0xFFFF00)
{
//TODO:
}
The above given code selects pixels with yellow color. Next step is to replace these selected pixels with new pixels. This is done by using the setPixel() method. This method takes three parameters x position of the pixel, y position of the pixel and the new color for the pixel.
if(bitmapData.getPixel(i,j)==0xFFFF00)
{
bitmapData.setPixel(i,j, 0xFF0000);
}
The above given code replaces all the yellow pixels with red ones, full code is given below.
for (var j = 0; j < bitmap.height; j++) {
for (var i = 0; i < bitmap.width; i++) {
if(bitmapData.getPixel(i,j)==0xFFFF00)
{
bitmapData.setPixel(i,j, 0xFF0000);
}
}
}
Below given is a sample application done with the above given code. Click on a color box and the color changes to red. The entire region is a single bitmap and only the selected pixels are replaced.
utility to find absolute or relative path
Apr 5th
Here is a little utility that can help you in finding, if a given url/path is relative or absolute. The method will handle all situation in both mac and windows.
Many of today’s dynamic web application runs with inputs from external sources like XML, web-services etc. Most of the cases, u need to handle lots of url from these external sources. below utility helps u in finding more details on any given url.
package com.browzor.base.utils { import flash.errors.IllegalOperationError; import flash.system.Capabilities; public class UrlUtilExtended { public static const PATH_TYPE_UNIDENTIFIED:Number = 0; public static const PATH_TYPE_RELATIVE:Number = 1; public static const PATH_TYPE_ABSOLUTE:Number = 2; // // // public function UrlUtilExtended():void { throw new IllegalOperationError("UrlUtilExtended: static class"); } public static function pathType(vUrl:String):Number { var currUrl:String = vUrl; var firstCharacterCode:Number = currUrl.charCodeAt(0); var osName:String = String(Capabilities.os); osName = osName.toLowerCase(); var osIsWindows:Boolean = osName.indexOf("windows") != -1; var osIsLinux:Boolean = osName.indexOf("linux") != -1; var osIsMacOS:Boolean = osName.indexOf("mac") != -1; // var startsWithForwardSlash:Boolean = firstCharacterCode == 47; /* (/) */ var startsWithBackwordSlash:Boolean = firstCharacterCode == 92; /* (\) */ var startsWithSemiImplicit:Boolean = firstCharacterCode == 42; /* (*) */ var startsWithColon:Boolean = firstCharacterCode == 58; /* (:) */ var startsWithDot:Boolean = firstCharacterCode == 46; /* (.) */ var startsWithTilt:Boolean = firstCharacterCode == 126; /* (~) */ var startsWithCharacter:Boolean = ((firstCharacterCode >= 48 && firstCharacterCode <= 57) || /* (0 - 1) */ (firstCharacterCode >= 65 && firstCharacterCode <= 90) || /* (A - Z) */ (firstCharacterCode >= 97 && firstCharacterCode <= 122) /* (a - z) */); // 8.3 flile name compliant var startsWithFileNameCompliantSpecialCharacter:Boolean = (firstCharacterCode == 36 || /* ($) */ firstCharacterCode == 37 || /* (%) */ firstCharacterCode == 39 || /* (') */ firstCharacterCode == 96 || /* (`) */ firstCharacterCode == 45 || /* (-) */ firstCharacterCode == 64 || /* (@) */ firstCharacterCode == 123 || /* ({) */ firstCharacterCode == 125 || /* (}) */ firstCharacterCode == 126 || /* (~) */ firstCharacterCode == 33 || /* (!) */ firstCharacterCode == 35 || /* (#) */ firstCharacterCode == 40 || /* (() */ firstCharacterCode == 41 || /* ()) */ firstCharacterCode == 38 || /* (&) */ firstCharacterCode == 95 || /* (_) */ firstCharacterCode == 94 /* (^) */); // win 95/98 additional complint var starsWithWinComplintSpecialCharacter:Boolean = (firstCharacterCode == 43 || /* (+) */ firstCharacterCode == 44 || /* (,) */ firstCharacterCode == 46 || /* (.) */ firstCharacterCode == 61 || /* (=) */ firstCharacterCode == 91 || /* ([) */ firstCharacterCode == 93 /* (]) */); var colonFound:Boolean = currUrl.indexOf(":") != -1; // // if (startsWithForwardSlash || startsWithBackwordSlash || (colonFound && startsWithCharacter) || startsWithSemiImplicit || startsWithTilt) return PATH_TYPE_ABSOLUTE else if (startsWithColon || startsWithDot || (!osIsWindows && startsWithCharacter) || /* check for other os than windows*/ (osIsWindows && (startsWithCharacter || startsWithFileNameCompliantSpecialCharacter || starsWithWinComplintSpecialCharacter)) /*check for win os*/) return PATH_TYPE_RELATIVE else return PATH_TYPE_UNIDENTIFIED; } } }
Usage :
Static constants UrlUtilExtended.PATH_TYPE_RELATIVE, UrlUtilExtended.PATH_TYPE_ABSOLUTE & UrlUtilExtended.PATH_TYPE_UNIDENTIFIED can be used for comparing the results
var url1:String = "http://www.44actions.com/"; var url2:String = "../content/image"; trace(UrlUtilExtended.pathType(url1)); // output : 2 trace(UrlUtilExtended.pathType(url2)); // output : 1
References:
- http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/mann/filename.ntcl.html
- http://www.westwind.com/reference/OS-X/paths.html
Flex 3 Drag from a TileList and drop on a Canvas
Mar 28th
In many projects I have worked on had some or the other way to implement a drag and drop mechanism in Flex i.e from one component to any other component. So I thought of posting a blog on that so that in future it would be easier for me as well as anyone who is trying to implement that.
Requirements:
- Flex 3
- A tile list filled up with some content.
- A canvas into which items has to be placed.
I expect the tile list to have an item renderer and say it has some kind of image displayed on it.
For example as below:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center" height="100%"
verticalGap="5" verticalAlign="middle" >
<mx:Image source="{data.icon}" mouseMove="dragIt(event)" width="60" height="60" maintainAspectRatio="true"/>
<mx:Label text="{data.name}" />
<mx:Spacer height="10"/>
</mx:VBox>
If you checkout the above xml you would find the image has a mouseMove event associated with it. Gets triggered when you click on the image and try to drag it.
Check out how the dragIt method has been implemented
private function dragIt(vEvent:MouseEvent):void
{
//Generating a bitmap data of the image being dragged
var dragInitiator:Image = vEvent.currentTarget as Image;
var bitmap_data:BitmapData=Bitmap(dragInitiator.content).bitmapData;
var bitmap:Bitmap = new Bitmap(bitmap_data);
// Create a DragSource object.
var dragSource:DragSource = new DragSource();
// Add the data to the object.
dragSource.addData(data, 'imagedata');
//Creating a proxy or ghost which will follow the mouse when dragged
var dragProxy:Image = new Image();
dragProxy.addChild(bitmap);
bitmap.width=100;
bitmap.height=100;
// Call the DragManager doDrag() method to start the drag.
DragManager.doDrag(dragInitiator, dragSource, vEvent, dragProxy);
}
That’s it now your item in the tile list is draggable. So we have finally achieved to create a dragable item for our title list. Now the interesting part comes when you try to drop on a targeted canvas. A dragged item appears like this screenshot below
![]()
In the above screenshot the dragged ghost of tileList item mouse pointer has a cross icon stating that if you drop it here nothing will happen. The item will animate back to the title list from where it was dragged.
Now the canvas in which the items has to be dropped should have event listeners associated with the dragged item.
Find the implementation below:
//Canvas container
var canvas:Canvas=new Canvas();
//setup the drop events to check if a valid asset has been added on it
//Fired when the user drops the dragged item on canvas
canvas.addEventListener(DragEvent.DRAG_DROP, onDragDrop);
//Fired when the dragged item exists from canvas
canvas.addEventListener(DragEvent.DRAG_EXIT, onDragExit);
//Fired when the dragged item enters the canvas
canvas.addEventListener(DragEvent.DRAG_ENTER, onDragEnter);
private function onDragEnter(vEvent:DragEvent):void
{
//This will remove the cross icon from the dragged item
//This will make the user feel like its a dropable area.
DragManager.acceptDragDrop(canvas);
}
There are other ways to achieve the above functionality. As this was one of my requirements in a project I just posted it out if it could make someone’s day easy. Please do comment if you think there is a better way to achieve this. I would rather be happy to update my post.
Loader complete event not triggering
Mar 23rd
This is a very common issue that a developer faces while using the Loader class. When I wrote my first loader in AS3 I did the same, I used
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
The above code doesn’t seems to have a mistake at the first look. But this wont trigger the onLoaderComplete when the file is loaded. The Event.COMPLETE needs to be attached to the contentLoaderInfo of the loader. Actual code should look like
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
How to create a Ranking Array from a Numeric Array?
Mar 22nd
Say you have an array of numbers [2,7,1,4,5,9,7] and you need to find a rank array for this like [5,1,6,4,3,0,2]. This array represents the rank of each item in the original array starting from 0, which represents the largest number in the array. Actionscript doesn’t provide a method to do this. But this can be done by using the sort method available in the Array class.
var originalArray:Array = [2,7,1,4,5,9,7];
var sortArrayIndex:Array = originalArray.sort(Array.NUMERIC | Array.DESCENDING | Array.RETURNINDEXEDARRAY);
var resultOrder:Array = new Array();
for (var f:int = 0; f < originalArray.length;f++)
{
resultOrder[sortArrayIndex[f]] = f;
}
trace(resultOrder); //5,1,6,4,3,0,2
If the array needs to be sorted ascending then the second line can be replaced with
var sortArrayIndex:Array = originalArray.sort(Array.NUMERIC | Array.RETURNINDEXEDARRAY);
FluorineFx
Mar 16th
![]()
A .net based media server supports the following features:
- Flex, Flash Remoting (RPC), Flex AMF Polling.
- Flex Messaging
- Flex Data Services (partial)
- Supports AMF0, AMF3 and RTMP, RTMPT protocols
- Service Browser
- Template based code generator (ASP.NET like syntax)
- Real-time Messaging, Remote SharedObject support, Video streaming
- MSMQ integration
- Easily integrate rich Internet applications with .NET backend
- Easily integrate with Adobe Integrated Runtime (Adobe AIR™)
Above all this is opensource
Check it out
http://fluorinefx.com/
A simple chat application using Adobe Stratus and Flex
Feb 8th
I thought of putting a tutorial on how I created a simple chat application which is possible using flash player 10 onwards. Believe me this does not require a costly media server to implement. Its very fast and communicates using Peer 2 Peer mechanism(P2P).
So what actually happens here is two flash player’s can communicate with each other directly. As shown in the picture below:

The above scenario would possibly erupt a volcano of ideas which can be possible with this kind of communication. Well I have already got many ideas related to developing an instant messenger or a multi player game etc. etc..
So I would just like to show how I developed a simple chat application which didn’t even took 15 minutes of my time to develop. So instead of wasting time reading this lets get into developing a chat application on the above technology.
Requirements:
- Adobe Stratus developer key
For details about Adobe Stratus http://labs.adobe.com/technologies/stratus/
For developer Key https://www.adobe.com/cfusion/entitlement/index.cfm?e=stratus - Flex SDK 3.2 or higher http://www.adobe.com/support/flex/downloads_updaters.html
- Flash Player 10
Before we continue I would like you to see what we are going to develop
You can find two instances of flash player below which from now onwards will be referred as FP 1 and 2 respectively. Enter the nick name [Textfield] in FP 1 and 2. My id will be a long string of alpha numeric characters. Copy and paste the my id of FP1 to FP2 send message to id text field. In the same way copy the my id of FP2 and paste it in the send message to id text field of FP 1. Press connect buttons in both FP1 and FP2. After few milliseconds the controls like message box, send textfield and send button will get enabled.
Now type some message in FP1 and press send, thats it messages will get transfered from FP1 to FP2. mmm.. a text chat application starts working.
FP 1
FP 2
Now lets see what happens inside. The code is well commented to make things clear
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
//The url to adobe stratus
private var mRtmfp_url:String="rtmfp://stratus.adobe.com";
private var mDeveloperKey:String=YOUR DEVELOPER KEY GOES HERE;
// Object of Net connection
private var mNetConnection:NetConnection;
//The client id generated by stratus is store in the bindable variable mId
[Bindable]
private var mId:String;
//The net stream for sending data
private var mSendStream:NetStream;
// The net stream for receiving data
private var mRecStream:NetStream;
public function init():void
{
connect_btn.addEventListener(MouseEvent.MOUSE_UP, connectPeer);
send_btn.addEventListener(MouseEvent.MOUSE_UP, sendMessage);
mNetConnection = new NetConnection();
mNetConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);
mNetConnection.connect(mRtmfp_url+"/"+mDeveloperKey);
}
private function netConnectionHandler(vEvent:NetStatusEvent):void
{
if(vEvent.info.code=="NetConnection.Connect.Success")
{
mId=mNetConnection.nearID;
mSendStream=new NetStream(mNetConnection, NetStream.DIRECT_CONNECTIONS);
mSendStream.addEventListener(NetStatusEvent.NET_STATUS,onSendStreamHandler);
mSendStream.publish("mystream");
}
}
private function sendMessage(vEvent:MouseEvent):void
{
if(send_message_txt.text=="")
{
}
else
{
var message_to_send:String="<b>"+myname_txt.text+": </b>"+send_message_txt.text+"<br/>";
mSendStream.send("reciveMessage", message_to_send);
message_box_txt.htmlText+=message_to_send;
send_message_txt.text="";
}
}
public function reciveMessage(vMessage:String):void
{
message_box_txt.htmlText+=vMessage;
}
private function connectPeer(vEvent:MouseEvent):void
{
if(send_id_txt.text=="")
{
Alert.show("Peer id cannot be empty!");
}
else
{
mRecStream = new NetStream(mNetConnection,send_id_txt.text);
mRecStream.addEventListener(NetStatusEvent.NET_STATUS,onReceiveStreamHandler);
mRecStream.play("mystream");
mRecStream.client = this;
}
}
private function onSendStreamHandler(vEvent:NetStatusEvent):void
{
}
private function onReceiveStreamHandler(vEvent:NetStatusEvent):void
{
if(vEvent.info.code=="NetStream.Play.Start")
{
message_box_txt.enabled=true;
message_box_txt.alpha=1;
send_message_txt.enabled=true;
send_message_txt.alpha=1;
send_btn.enabled=true;
send_btn.alpha=1;
}
}
]]>
</mx:Script>
<mx:VBox paddingLeft="10" paddingTop="10" paddingRight="10" paddingBottom="10" width="100%" height="100%">
<mx:Label text="Stratus demo text chat application"/>
<mx:HBox width="100%">
<mx:Label text="Nick name:"/>
<mx:TextInput id="myname_txt" width="50%" />
</mx:HBox>
<mx:HBox width="100%">
<mx:Label text="My id is"/>
<mx:Label text="{mId}" selectable="true"/>
</mx:HBox>
<mx:HBox width="100%">
<mx:Label text="Send message to id"/>
<mx:TextInput id="send_id_txt" width="70%" />
<mx:Button id="connect_btn" label="Connect" />
</mx:HBox>
<mx:TextArea id="message_box_txt" enabled="false" alpha="0.5" width="100%" height="60%"/>
<mx:HBox width="100%">
<mx:TextInput width="70%" enabled="false" alpha="0.5" id="send_message_txt" />
<mx:Button label="send" enabled="false" alpha="0.5" id="send_btn"/>
</mx:HBox>
</mx:VBox>
</mx:Application>
Test Driven Development in FB4
Feb 4th
Flash Platform lacked a proper TDD development tool for a long time. Adobe has taken this seriously and integrated it with the Flex Builder 4. See tutorial at the below given link.
http://www.adobe.com/devnet/flex/articles/flashbuilder4_tdd.html
