5 useful ActionScript 3 tips
Sometimes, when you work on some project, you find something that is small, but very interesting and useful. Usually I'm adding such kind of things in classes that I like to call helpers. The following tips are part of my helpers.
Change the fill color of a MovieClip
function setMovieClipColor(clip: MovieClip, color: uint): void {
var ct: ColorTransform = new ColorTransform();
ct.color = color;
clip.transform.colorTransform = ct;
}
Convert color from uint to hex
var color:uint = 0xff0000;
trace(color.toString(16)); // -> ff0000;
Remove all the children of a MovieClip
public
function removeAllChildren(movie: MovieClip): void {
var numOfChilds: int = movie.numChildren;
while (movie.numChildren > 0) {
if (movie.getChildAt(0)) {
if (movie.contains(movie.getChildAt(0))) {
movie.removeChild(movie.getChildAt(0));
}
}
}
}
Delegation
public function create(handler: Function, ...args): Function {
return function (...innerArgs): void {
handler.apply(this, innerArgs.concat(args));
}
}
Date validation
public function validateDate(day: Number, month: Number, year: Number): Boolean {
var date: Date = new Date(year, month, day);
if (date.getMonth() == month) {
return true;
} else {
return false;
}
}