Thursday, June 3, 2010

Phythagorean Theorem - Distance Formula

Sum of the Squares of the two legs of a right triangle is equal to the square of the hypotenuse.

A^2 + B^2 = C^2


var dist:Number;
var circle:Circle;

function onStageEnter(event) {
var dx:Number = event.target.x - mouseX;
var dy:Number = event.target.y - mouseY;
dist = Math.sqrt(dx*dx + dy*dy);
event.target.alpha = 1-(dist/250);
distText.text = String(dist);
distText.x = mouseX;
distText.y = mouseY+20;
}

for (var i:int = 1; i<=7; i++) {

 for (var j:int = 1; j<=5; j++) {

  circle = new Circle();
  circle.x = (i*circle.width)-circle.width;
  circle.y = (j*circle.height)-circle.height;
  circle.addEventListener(Event.ENTER_FRAME, onStageEnter);
  addChild(circle);
 }
}



Friday, May 28, 2010

Math - Rotate MovieClip to towards Mouse

Tuesday, April 27, 2010

Numeric Puzzle Game !

You have a surprise at the end of the game !!

Tuesday, March 9, 2010

Learn Actionscirpt Techniques - ColorPicker Gradient



The ColorPicker component displays a list of one or more swatches from which the user can select a color.

By default, the component displays a single swatch of color on a square button. When the user clicks this button, a panel opens to display the complete list of swatches.

// Here is the Source Code

var startHex;
var endHex;
var spacing:int = 25;
var noOfBoxes:int = 13;
function fadeHex(ratio) {
var r = startHex >> 16;
var g = startHex >> 8 & 0xFF;
var b = startHex & 0xFF;
r += ((endHex >> 16 & 0xFF)-r)*ratio;
g += ((endHex >> 8 & 0xFF)-g)*ratio;
b += ((endHex & 0xFF)-b)*ratio;
return (r << 16 | g << 8 | b);
}
function drawSquare(x, y, color) {
graphics.beginFill(color);
graphics.moveTo(x,y);
graphics.lineTo(x+50,y);
graphics.lineTo(x+50,y+50);
graphics.lineTo(x,y+50);
graphics.lineTo(x,y);
}
function colorChange(e:Event) {
var cp:ColorPicker = e.target as ColorPicker;
if (cp==cp1) {
startHex = "0x"+e.target.hexValue ;
} else {
endHex = "0x"+e.target.hexValue;
}
for (var i = 0; i
var ratio = i/noOfBoxes;
var xx = spacing+(i*50);
var yy = 70;
var nowColor = fadeHex(ratio);
drawSquare(xx,yy,nowColor);
}
}
cp1.selectedColor = 0x990000;
cp2.selectedColor = 0x000000;
cp1.addEventListener(Event.CHANGE, colorChange);
cp2.addEventListener(Event.CHANGE, colorChange);
cp1.addEventListener(Event.RENDER, colorChange);
cp2.addEventListener(Event.RENDER, colorChange);

Monday, March 8, 2010

AS3 Naming Conventions

Use precise, meaningful, contextually relevant names that are as descriptive as possible. Limit the use of unnecessary abbreviations.

Example: defaultImagePreview
NOT: dfltImgPrv


Start each instance name with a lowercase letter, and intercap the remaining words.

Examples: menuItem, sectionTitle

Apply the same conventions to variable names.

Examples: accountNumber, startingPoint, currentProductName

Begin a class name with an uppercase letter. Write class names in mixed case when it’s a compound or concatenated word. Keep package names as short as possible, using reverse domain naming conventions.

Examples: com.client.project.component.ClassName

Name functions and methods in the form of verbs, since they perform actions.

Examples: getUserName(), calculateTaxes(), createSubMenu()

Name booleans with an appropriate, positive prefix, such as “is”, “has”, “can”, or “should”, for an easier determination between true and false.

Examples: isSoundOn, isFinished, isOpen, isLoggedIn, hasLicense, canEvaluate

Use complement names for complement entities: get/set, add/remove, create/destroy, start/stop, insert/delete, increment/decrement, old/new, begin/end, first/last, up/down, min/max, next/previous, open/close, show/hide.

Examples: getUserName, setUserName
addThumbnailImage, removeThumbnailImage
showAnimation, hideAnimation


Private variable names may be prefixed with an underscore or $ symbol as long as consistency is maintained.

Monday, February 15, 2010

Learn Actionscript Techniques - Change Text Color in Runtime




import flash.text.TextFormat;

var format:TextFormat = new TextFormat();
var r:Number = 0x000000;
var g:Number = 0x000000;
var b:Number = 0x000000;
var red:Number;
var green:Number;
var blue:Number;

addEventListener(Event.ENTER_FRAME,changeColor);

function changeColor(evt:Event) {
red = 127*(1+Math.cos(r += .03));
green = 127*(1+Math.cos(g += .06));
blue = 127*(1+Math.cos(b += .02));
var myColor = red << 16 | green << 8 | blue;
format.color = myColor;
colorText.setTextFormat(format);
}

Bitwise Operation in Flash

Bitwise gems - fast integer math

Bitwise operators are very fast in AS3, so here is a small collection of code snippets which can speed up certain computations. I won’t explain what bit operators are and how to use them, rather pointing to an excellent article hosted on gamedev.net: ‘Bitwise Operations in C’.
If you know any good tricks that are not included here, feel free to leave a comment or send me an email. All benchmarks were done in AS3.

Left bit shifting to multiply by any power of two

Approximately 300% faster.

x = x * 2; 
x = x * 64;  
//equals: 
x = x << 1; 
x = x << 6;

Right bit shifting to divide by any power of two

Approximately 350% faster.

x = x / 2; 
x = x / 64;  
//equals: 
x = x >> 1; 
x = x >> 6;


Number to integer conversion

Using int(x) is 10% faster in AS3. Still the bitwise version works better in AS2.

x = int(1.232)  
//equals: 
x = 1.232 >> 0;

Extracting color components

Not really a trick, but the regular way of extracting values using bit masking and shifting.

//24bit 
var color:uint = 0x336699;
var r:uint = color >> 16;
var g:uint = color >> 8 & 0xFF;
var b:uint = color & 0xFF;  
//32bit 
var color:uint = 0xff336699;
var a:uint = color >>> 24;
var r:uint = color >>> 16 & 0xFF;
var g:uint = color >>>  8 & 0xFF;
var b:uint = color & 0xFF;

Combining color components

‘Shift up’ the values into the correct position and combine them.

//24bit 
var r:uint = 0x33;
var g:uint = 0x66;
var b:uint = 0x99;
var color:uint = r << 16 | g << 8 | b;  
//32bit 
var a:uint = 0xff;
var r:uint = 0x33;
var g:uint = 0x66;
var b:uint = 0x99;
var color:uint = a << 24 | r << 16 | g << 8 | b;

Swap integers without a temporary variable using XOR

Pretty neat trick, it is explained in detail in the link at the top of the page. This is 20% faster.

var t:int = a; a = b; b = t;  
//equals: 
a ^= b; 
b ^= a; 
a ^= b;

Increment/decrement

This is much slower than the pre/post decrement operator, but a nice way to obfuscate your code.

i = -~i; 
// i++ 
i = ~-i; 
// i--

Sign flipping using NOT or XOR

Strangely enough, this is 300%(!) faster.

i = -i;  
//equals 
i = ~i + 1;  
//or 
i = (i ^ -1) + 1;

Fast modulo operation using bitwise AND

If the divisor is a power of 2, the modulo (%) operation can be done with:
modulus = numerator & (divisor - 1);
This is about 600% faster.

x = 131 % 4;  
//equals: 
x = 131 & (4 - 1);

Check if an integer is even/uneven using bitwise AND

This is 600% faster.

isEven = (i % 2) == 0;  
//equals: 
isEven = (i & 1) == 0;

Absolute value

Forget Math.abs() for time critical code. Version 1 is 2500% faster than Math.abs(), and the funky bitwise version 2 is again 20% faster than version 1.

//version 1 
i = x < 0 ? -x : x;  
//version 2 
i = (x ^ (x >> 31)) - (x >> 31);

Comparing two integers for equal sign

This is 35% faster.

eqSign = a * b > 0;  
//equals: 
eqSign = a ^ b >= 0;

Fast color conversion from R5G5B5 to R8G8B8 pixel format using shifts

R8 = (R5 << 3) | (R5 >> 2) G8 = (R5 << 3) | (R5 >> 2) B8 = (R5 << 3) | (R5 >> 2)


--

Thursday, January 7, 2010

Missing Passport - Small Idea Small Blog

If you find any important document like Driving Licence, Ration Card, Passport, Bank Passbook Etc., missed by someone, simply put them into near by any POSTBOXES. They'll automatically reach the owner, and fine will be collected from them.

Tuesday, January 5, 2010

Learn Flash ActionScript Techniques - Round Nearest Decimal Place

There are numerous reasons to round numbers. For example, when displaying the results of a calculation, you might display only the intended precision.Because all arithmetic in ActionScript performed with floating-point numbers, some calculations result in unexpected floating-point numbers that must be rounded.For example, the result of a calculation may be 3.99999999 in practice, even though it should be 4.0 in theory.

The Math.round() method returns the nearest integer value of any parameter passed to it

trace(Math.round(29.577)); // Displays: 29
trace(Math.round(189.697)); // Displays: 189

The Math.floor() method rounds down, and the Math.ceil() rounds up:

trace(Math.floor(204.99)); // Displays: 204
trace(Math.floor(401.01)); // Displays: 401


To round a number to the nearest decimal place:

1. Decide the number of decimal places to which you want the number rounded. For example, if you want to round 90.337 to 90.34 then you want to round to two decimal places, which means you want to round to the nearest .01.

2. Divide the input value by the number chosen in Step 1 (in this case, .01).

3. Use Math.round() to round the calculated value from Step 2 to the nearest integer.

4. Multiple the result of Step 3 by the same value that you used to divide in Step 2.

For example, to round 90.337 to two decimal places, you could use:

trace(Math.round(90.337 / .01) * .01); // Displays: 9.34

You can use the identical math to round a number to the nearest multiple of an integer.

For example, this rounds 92.5 to the nearest multiple of 5:

trace(Math.round(92.5 / 5) * 5); // Displays: 95

Saturday, January 2, 2010

New Year Celebration 2010

Lot of Excitement with my team members (C2 Workshop Pvt Ltd)
















Thirukkural