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.

Thirukkural