Powered By

Free XML Skins for Blogger

Powered by Blogger

As3 Key combinations

| Monday, November 9, 2009

Came across an interesting utility related to key combinations in AS3


Will be of great help when developing keyboard based games or apps.

As3 Character code (charcode) to Character

|

Came across a code which can convert character code to character.

this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);


function onKeyDown(vEvent:KeyboardEvent):void
{
trace(String.fromCharCode(vEvent.charCode));
}

Flex Image to Bitmap converter

| Saturday, November 7, 2009

Today I came across a problem where I required to convert an Image datatype to bitmap so that I can duplicate the dynamically loaded image.



//test_image is an object of image
var bitmap_data:BitmapData=Bitmap(test_image.content).bitmapData;
var bitmap:Bitmap = new Bitmap(bitmap_data);


Thats it the bitmap is ready :)

Converting and XMLList to XMLListCollection

| Wednesday, November 4, 2009

// Say you have an XMLList object "xml_list"
//To convert it to XMLListCollection just do the below
var xmlListcol:XMLListCollection=new XMLListCollection(xmllist);

AS3 Vector

| Tuesday, November 3, 2009

A vector elements should be of same data type.

var numVector:Vector.=new Vector.();
numVector.push(2,17,6.6);

var sortedVector:Vector.=numVector.sort(sortNumbers);

function sortNumbers(x:Number, y:Number):Number
{
// do the sorting here

}

Embed assets in flash cs4 through code

|

[Embed(source="/assets/flex4.png")]
var imageData:Class;

//Other property of embed
//[Embed(source="/assets/flex4.xml“, mimeType=“application/octet-stream”)]


var disObj:DisplayObject=new imageData();
addchild(disobj)

When you compile a dialogue will be displayed to select the flex sdk location.
The first time it will not work.

Compile it again to see it working.

As3 click and zoom to the mouse position

| Tuesday, October 13, 2009

From quite a long time I used to face an issue when it comes to zooming a movie clip to the position where the mouse has been clicked and finally managed to find a decent solution.

Find a sample below:




The coding part:

image_mc.addEventListener(MouseEvent.MOUSE_UP, onImageClick);

function onImageClick(vEvent:MouseEvent):void
{

var cur_mx:Number=image_mc.mouseX;
var cur_my:Number=image_mc.mouseY;


image_mc.scaleX*=2;
image_mc.scaleY*=2;

image_mc.x=(cur_mx-(cur_mx*image_mc.scaleX));
image_mc.y=(cur_my-(cur_my*image_mc.scaleY));
}

AS3 Local to Global --- localToGlobal() function

| Monday, October 12, 2009

Take the below scenairo where in the stage we have a main movieclip which contains two movieclips. A circle_mc and a rectangle_mc. I want to determine the stage position of the circle_mc.


To achieve this we have to use the localToGlobal method found in a movieclip object.
In the above scenario the code goes as below:
//The variable to store the target location required
var result_point:Point=new Point();

//a point object created for storing the current location of circle_mc
var circle_point:Point=new Point(map_mc.circle_mc.x, map_mc.circle_mc.y);

//using the localToGlobal method for calculating the current global position of circle.
result_point=map_mc.circle_mc.localToGlobal(circle_point);