Jun 30
Continuously Scrolling Thumbs to Grid View
cool little example i had to make for a gallery, it switches between continuously scrolling thumbnails to a grid view making it easier to find thumbnails.
Source 2 comments
Apr 20
MC Bouncing with Rotation
A cool remake of an example done by Keith Peters (bit-101.com) converted to AS3.
Click and Drag below.
Source
Sample Code:
....
if (! this._mcWeight.dragging) {
trace("if");
this._mcWeight.vr+=this._mcWeight.rotation*-.1;
this._mcWeight.vr*=.9;
this._mcWeight.rotation+=this._mcWeight.vr;
this._mcWeight.vy+=1;
this._mcWeight.vx*=.98;
this._mcWeight.vy*=.98;
this._mcWeight.x+=this._mcWeight.vx;
this._mcWeight.y+=this._mcWeight.vy;
if (this._mcWeight.y>360) {
this._mcWeight.y=360;
this._mcWeight.vy*=-.6;
this._mcWeight.vx*=.9;
this._mcWeight.vr+=this._mcWeight.vx*Math.PI;
if (Math.abs(this._mcWeight.rotation)>95) {
this._mcWeight.vy-=this._mcWeight.vr/Math.PI*.5;
this._mcWeight.vr*=.5;
}
}
if (this._mcWeight.x>540) {
this._mcWeight.x=0;
}
if (this._mcWeight.x<0) {
this._mcWeight.x=540;
}
} else {
trace("else");
this._mcWeight.vx=this._mcWeight.x-this._mcWeight.oldX;
this._mcWeight.vy=this._mcWeight.y-this._mcWeight.oldY;
this._mcWeight.oldX=this._mcWeight.x;
this._mcWeight.oldY=this._mcWeight.y;
}
....
No comments
Feb 13
Removing Duplicate Objects from Array
I tweeked the previous function to now remove duplicate objects, in this example its looking for the node “title” (lines 14 and 16) inside the object, once it finds the duplicate it removes it with splice.
Sample Code:
//
var arr:Array = new Array;
arr.push({title:"Test1"});
arr.push({title:"Test2"});
arr.push({title:"Test1"});
arr.push({title:"Test3"});
removeDup(arr);
function removeDup($arr:Array):Array {
var arrTemp:Array = $arr;
for (var y:Number = 0; y < arrTemp.length; y++) {
var arrTemp2 = arrTemp[y]["title"];
for (var z:Number = (y+1); z < arrTemp.length; z++) {
if (arrTemp2==arrTemp[z]["title"]) {
arrTemp.splice(z,1);
}
}
}
return arrTemp;
}
//
No comments
Feb 13
Removing Duplicate Items in an Array
Quick function to remove duplicates in an array:
How it Works:
create a for loop to run through the array, checking to see if there is any duplicates in the array, if there is splices it
Sample Code:
//
var arr:Array = ["2", "3", "4", "6", "6"];
trace(arr) // 2,3,4,6,6
removeDup(arr)
trace(arr) // 2,3,4
function removeDup($arr:Array):Array {
var arrTemp:Array = $arr;
for (var y=0; y < arrTemp.length; y++) {
var arrTemp2 = arrTemp[y];
for (var z=(y+1); z < arrTemp.length; z++) {
while (arrTemp2 == arrTemp[z]) {
arrTemp.splice(z,1);
}
}
}
return arrTemp;
}
1 comment
Jan 22
Flash Glow Filter vs TweenMax Glow Filter
this example has two dynamic text fields with glow filters one using Flash GlowFilter, and the other TweenMax glowFilter. As you can see by the example below the tweenMax function is a lot easier and a lot cleaner.
Source
Sample Code:
...
/*
apply Tween Glow
*/
private function applyTweenEffect($mc:MovieClip):void
{
TweenMax.to($mc, .5, {glowFilter:{color:0xff6600, alpha:1, blurX:10, blurY:10}});
}
...
...
/*
apply Filter Glow
*/
private function applyFilterEffect($mc:MovieClip):void
{
var arrFilter:Array = new Array();
var glowFilter:GlowFilter = new GlowFilter(0xFF6600,.5,10,10,2)
arrFilter.push(glowFilter);
$mc.filters = arrFilter
}
1 comment
Jan 22
Form Validation Functions
Creating forms and having them validate can be really time consuming, unless….. you have classes or functions that you’ve previously built that ou can reuse (makes your life alot easier) so ive added some really basic functions that can be used in validating forms (or anything else).
Sample Code:
public static function isEmail($email:String):Boolean
{
if (typeof($email) == "string") {
if ($email.length < 6 || $email.indexOf(".") <= 0 || $email.indexOf(",") >= 0 || $email.indexOf(";") >= 0 || $email.indexOf(":") >= 0 || $email.indexOf("/") >= 0 || $email.indexOf(" ") >= 0 || $email.indexOf("@") <= 0 || $email.indexOf("@") != $email.lastIndexOf("@") || $email.lastIndexOf(".") < $email.indexOf("@") || $email.lastIndexOf(".") + 3 > $email.length) {
return false;
} else {
return true;
}
} else {
return false;
}
}
/*
Validate Number
*/
public static function isNumber($number:Number):Boolean
{
var numNumber:Number = Number($number);
if (isNaN($number)) {
return false;
} else {
return true;
}
}
/*
Validate String
*/
public static function isString($string:String):Boolean
{
if (typeof $string != "string") {
return false;
} else {
return true;
}
}
/*
Validate if a field is blank
*/
public static function isBlank($var:*):Boolean
{
var strCheck:String = String($var);
if (strCheck.length >= 1) {
return false;
} else {
return true;
}
}
/*
Validate Number Range
*/
public static function isInRange($number:Number, $min:Number, $max:Number):Boolean
{
if ($number >= $min && $number <= $max) {
return true;
} else {
return false;
}
}
/*
Validate String
*/
public static function isLength($var:*, $length:Number):Boolean
{
var strCheck:String = String($var);
if (strCheck.length == $length) {
return true;
} else {
return false;
}
}
No comments
Nov 24
Making Preload Play Everytime
When a client wants to have the preloader show everytime the user refreshes the page (then its not really a preloader, its a movie…lol ) never the less i came up with this cool little quick fix.
have (2) boolean variables:
(1)_bolEndLoader (this will keep track of the preloader, once the preloader has finished loading it will be set to true).
(2)_bolEndAnim (will keep track of the animation, once the animation has ended it will be set to true).
then in the “outro” function do a check to make sure both boolean variables are “True” is they are play site. if they’re not it will just wait till either the animation of the preloader to finish.
Sample Code:
...
public function displayProgress($percent:uint):void
{
if($percent == 100)
{
this._bolEndLoader = true;
this.outro();
}
//use the tweener to tween to the final frame in 4 secs. then onComplete play function "playPreloader"
Tweener.addTween(this.mcPreloader, {_frame:this.mcPreloader.totalFrames, time:4, transition:"linear", onComplete:this.playPreloader});
}
private function playPreloader():void
{
this._bolEndAnim = true
this.outro();
}
public function outro($properties:Object=null):void
{
trace("PRELOADER OUTRO")
if((this._bolEndAnim == true) && (this._bolEndLoader == true))
{
Tweener.addTween(this.mcPreloader, {alpha:0, time:.3, transition:"linear", onComplete:this.outroComplete});
}
else
{
//do nothing...both not true
}
}
....
No comments
Nov 6
Panning an Image
Quick example of an image panning, that uses “updateAfterEvent” to help make the animation less “choppy”
Source
Sample Code Sample:
....
...
private function applyMovement():void
{
this.ease(this.calculatePosition(), this._numSpeed);
}
/*
Panning Functionality
*/
private function calculatePosition():Number
{
this._numPosition -= this.calculateIncrement();
if(this._numPosition > this._numLeftMax) {
this._numPosition = this._numLeftMax
} else if (this._numPosition < this._numRightMax) {
this._numPosition = this._numRightMax
}
return this._numPosition;
}
//
private function calculateIncrement():Number
{
var numDistance:Number = this.calculateDistance();
var numIncrement:Number = numDistance / this._numIncrementRatio;
return numIncrement;
}
//
private function calculateDistance():Number
{
var numDistance:Number = this.getMousePosition() - this._numCenter;
return numDistance;
}
private function getMousePosition():Number
{
return this.mouseX;
}
private function ease($position:Number, $speed:Number):void
{
Math.round(this.mcFloorPlan.x += ($position - this.mcFloorPlan.x) / $speed)
}
....
....
2 comments
Aug 26
Multiple-Object Collision Detection
The Example below is from the book: Making Things Move, by Keith Peters, it is using six sprites.
sprite0 with sprite1, sprite2, sprite3, sprite4, sprite5
sprite1 with sprite2, sprite3, sprite4, sprite5
sprite2 with sprite3, sprite4, sprite5
sprite3 with sprite4, sprite5
sprite4 with sprite5
sprite5 with nothing
You’ll see in the first round of tests, you’re testing sprite0 with every other sprite. So no other sprite needs to test against sprite0 again. So yo can drop sprite0 off the list. then sprite1 tests against the remaining sprites, and yo can drop it off the list. By the time you get to the last one, sprite5, every other sprite has already tested itself for a collision with it. So theres no need to test it against anything.
Sample Code:
numSprites = 6;
for(var i:uint = 0; i < numSprites - 1; i++)
{
spriteA = sprites[i];
for(var j:uint = i + 1; j < numSprites; j++)
{
spriteB = sprites[j];
if(spriteA.hitTestObject(spriteB))
{
//do whatever
}
}
}
The outer loop goes to one less than the total number of clips, because you dont need to test the last clip against anything.The inner loop, you always start with one higher than the index of the outer loop, because you've already tested everything lower, and you dont want to test the same index against itself.
No commentsAug 11
String to Array
Function that takes a String, and stores each word in an Array
ex:
Sample Code:
//
var strTemp = "this is a string to array test"
this.stringToArray(strTemp);
//output
// strTemp = ["this", "is", "a", "string", "to", "array", "test"]
private function stringToArray($string:String):Array
{
var a = $string.split(" ");
var temp = [];
for (var x = 0; x < a.length; x++) {
temp.push(a[x]);
}
return temp;
}
3 comments