Flex.org already has tons of tutorials and looks like a great resource for Flex development and the Flex community.

flex.org
I was first introduced to the ColorTransform class while reading the AS2 to AS3 migration documentation about setRGB(). I was immediately impressed with the features, stomach web but intimidated when I saw the constructor and all the offset, story color and alpha arguments. I just needed to change the color of something, nothing special, no gradients, no alpha, no space shuttle landings, just a simple hex color change.

So here is a simple color change utility that is a lot closer to what I am used to.

?View Code ACTIONSCRIPT
function paint(obj:Object,color:Number):void{
var ct:ColorTransform = new ColorTransform();
ct.color = color;
obj.transform.colorTransform = ct;
}



source here
I was first introduced to the ColorTransform class while reading the AS2 to AS3 migration documentation about setRGB(). I was immediately impressed with the features, stomach web but intimidated when I saw the constructor and all the offset, story color and alpha arguments. I just needed to change the color of something, nothing special, no gradients, no alpha, no space shuttle landings, just a simple hex color change.

So here is a simple color change utility that is a lot closer to what I am used to.

?View Code ACTIONSCRIPT
function paint(obj:Object,color:Number):void{
var ct:ColorTransform = new ColorTransform();
ct.color = color;
obj.transform.colorTransform = ct;
}



source here
Here is an AS2 version of the SuperLoader. It works just like the AS3 version found in the Custom HTML Templates post.

Quick Use:
1. Add loaderbar.swf and mainLoader.swf to your current project directory.

2. In the html file for your current project, pilule replace the src and movie parameters to mainLoader.

3. Then add [, website like this 'flashVars', stomach 'swfURL=FlashProjectToLoad.swf'] to the load script.

Example for AC_RunActiveContent:

AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
'width', '472',
'height', '280',
'src', 'mainLoader',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
'play', 'true',
'loop', 'true',
'scale', 'showall',
'wmode', 'window',
'devicefont', 'false',
'id', 'mainLoader',
'bgcolor', '#000000',
'name', 'mainLoader',
'menu', 'true',
'allowFullScreen', 'false',
'allowScriptAccess','sameDomain',
'movie', 'mainLoader',
'salign', '',
'flashVars','swfURL=imgViewer.swf'
); //end AC code

Example for HTML Object/Embed:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="472" height="280" id="mainLoader" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="mainLoader.swf?swfURL=imgViewer.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="mainLoader.swf?swfURL=imgViewer.swf" quality="high" bgcolor="#ffffff" width="472" height="280" name="mainLoader" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

4. If your frame rate is different then 30, you will need to update the frame rate in the mainLoader.fla file.

5. If you are using this loader inside of your Flash movie, you may also need to comment out the line that sets _root to the loading file [mainMC._lockroot = true;].

Demo:

Source Here

UPDATE: 1.1

1. Added usage information, the class could be used as a simple loader monitor if needed.
2. Moved class path from com.hdi.util.HDLoader to com.hdi.as2.HDLoader.

See just the loader class below.

?View Code ACTIONSCRIPT
/**********************************************************
HDLoader is a simple loader monitor that can attach to your custom loading bar
version 1.1
date: 1-9-10
 
How To Use:
1. Have something that needs loading
 
2. Create a function to handle the loading monitor updates. Should look something like this.
function loaderUpdate(perc:Number){
// perc is the percent loaded in non decimal form. 0 - 100.
// you can make a loading bar move or scale like so
loaderBar._xscale = perc/100;
 
// you can display text of the loading status.
loaderText.text = "LOADING: " + perc + "%";
}
 
3. Create a funciton to handle when loading is complete
function loadComplete(){
// hiding the loader after loading is always a good thing.
loaderBar._visible = false;
 
// do what ever you want here, your crap is now loaded.
}
 
4. import HDLoader.
import com.hdi.as2.HDLoader;
 
5. Start the loader, placing the correct target references in the correct argument positions.
arg 1: the MovieClip doing the laoding.
arg 2: the loader update function created in step 2.
arg 3: the loader complete function created in step 3.
 
// we are assuming you have just started loading something (mc.loadMovie("someSWF.swf");
var loaderMon:HDLoader = new HDLoader(mc,loaderUpdate,loadComplete);
 
Author: Todd Williams
website: www.taterboy.com
website: hdinteractive.com
***********************************************************/
 
class com.hdi.as2.HDLoader{
var targMC:Object;
var monFunc:Function;
var finFunc:Function;
public function HDLoader(targ,mfunc,func){
init(targ,mfunc,func);
}
private function init(targ,mfunc,func){
var lbytes,tbytes,perc;
var frameParent:Object = targ._parent;
var cnt:Number = 3;
var frameObj:MovieClip = frameParent.createEmptyMovieClip("frameLdr",frameParent.getNextHighestDepth());
frameObj.onEnterFrame = function(){
lbytes = targ.getBytesLoaded();
tbytes = targ.getBytesTotal();
perc = Math.round((lbytes/tbytes)*100);
if (lbytes < tbytes) {
if(mfunc != null){
mfunc(perc);
}
}
else if(tbytes > 200){
if(cnt == 0){
if(func != undefined){
func();
}
delete frameObj.onEnterFrame;
}
cnt --;
if(mfunc != null){
mfunc(100);
}
}
}
}
}

I was first introduced to the ColorTransform class while reading the AS2 to AS3 migration documentation about setRGB(). I was immediately impressed with the features, stomach web but intimidated when I saw the constructor and all the offset, story color and alpha arguments. I just needed to change the color of something, nothing special, no gradients, no alpha, no space shuttle landings, just a simple hex color change.

So here is a simple color change utility that is a lot closer to what I am used to.

?View Code ACTIONSCRIPT
function paint(obj:Object,color:Number):void{
var ct:ColorTransform = new ColorTransform();
ct.color = color;
obj.transform.colorTransform = ct;
}



source here
Here is an AS2 version of the SuperLoader. It works just like the AS3 version found in the Custom HTML Templates post.

Quick Use:
1. Add loaderbar.swf and mainLoader.swf to your current project directory.

2. In the html file for your current project, pilule replace the src and movie parameters to mainLoader.

3. Then add [, website like this 'flashVars', stomach 'swfURL=FlashProjectToLoad.swf'] to the load script.

Example for AC_RunActiveContent:

AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
'width', '472',
'height', '280',
'src', 'mainLoader',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
'play', 'true',
'loop', 'true',
'scale', 'showall',
'wmode', 'window',
'devicefont', 'false',
'id', 'mainLoader',
'bgcolor', '#000000',
'name', 'mainLoader',
'menu', 'true',
'allowFullScreen', 'false',
'allowScriptAccess','sameDomain',
'movie', 'mainLoader',
'salign', '',
'flashVars','swfURL=imgViewer.swf'
); //end AC code

Example for HTML Object/Embed:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="472" height="280" id="mainLoader" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="mainLoader.swf?swfURL=imgViewer.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="mainLoader.swf?swfURL=imgViewer.swf" quality="high" bgcolor="#ffffff" width="472" height="280" name="mainLoader" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

4. If your frame rate is different then 30, you will need to update the frame rate in the mainLoader.fla file.

5. If you are using this loader inside of your Flash movie, you may also need to comment out the line that sets _root to the loading file [mainMC._lockroot = true;].

Demo:

Source Here

UPDATE: 1.1

1. Added usage information, the class could be used as a simple loader monitor if needed.
2. Moved class path from com.hdi.util.HDLoader to com.hdi.as2.HDLoader.

See just the loader class below.

?View Code ACTIONSCRIPT
/**********************************************************
HDLoader is a simple loader monitor that can attach to your custom loading bar
version 1.1
date: 1-9-10
 
How To Use:
1. Have something that needs loading
 
2. Create a function to handle the loading monitor updates. Should look something like this.
function loaderUpdate(perc:Number){
// perc is the percent loaded in non decimal form. 0 - 100.
// you can make a loading bar move or scale like so
loaderBar._xscale = perc/100;
 
// you can display text of the loading status.
loaderText.text = "LOADING: " + perc + "%";
}
 
3. Create a funciton to handle when loading is complete
function loadComplete(){
// hiding the loader after loading is always a good thing.
loaderBar._visible = false;
 
// do what ever you want here, your crap is now loaded.
}
 
4. import HDLoader.
import com.hdi.as2.HDLoader;
 
5. Start the loader, placing the correct target references in the correct argument positions.
arg 1: the MovieClip doing the laoding.
arg 2: the loader update function created in step 2.
arg 3: the loader complete function created in step 3.
 
// we are assuming you have just started loading something (mc.loadMovie("someSWF.swf");
var loaderMon:HDLoader = new HDLoader(mc,loaderUpdate,loadComplete);
 
Author: Todd Williams
website: www.taterboy.com
website: hdinteractive.com
***********************************************************/
 
class com.hdi.as2.HDLoader{
var targMC:Object;
var monFunc:Function;
var finFunc:Function;
public function HDLoader(targ,mfunc,func){
init(targ,mfunc,func);
}
private function init(targ,mfunc,func){
var lbytes,tbytes,perc;
var frameParent:Object = targ._parent;
var cnt:Number = 3;
var frameObj:MovieClip = frameParent.createEmptyMovieClip("frameLdr",frameParent.getNextHighestDepth());
frameObj.onEnterFrame = function(){
lbytes = targ.getBytesLoaded();
tbytes = targ.getBytesTotal();
perc = Math.round((lbytes/tbytes)*100);
if (lbytes < tbytes) {
if(mfunc != null){
mfunc(perc);
}
}
else if(tbytes > 200){
if(cnt == 0){
if(func != undefined){
func();
}
delete frameObj.onEnterFrame;
}
cnt --;
if(mfunc != null){
mfunc(100);
}
}
}
}
}

Passing Flash variables to Javascript, online passing Javscript variables to Flash, price Calling Flash functions from Javascript.

There are a few conventions for getting Flash and Javascript to communicate like fscommand, getURL, LocalConnection, URL-encoded variables, FlashVars, SetVariable and TCallLabel to name a few.

Here is a little demo using Flash's External Interface and Javascript. It is more elegant then most of the procedures I have used in the past. Between External Interface and FlashVars, you should be able to fill most of your needs.

By the way, the syntax is almost exactly the same for AS2 and AS3.

Flash Usage:

?View Code ACTIONSCRIPT
//import flash.external.*; //for AS2
 
//1. calling javascript function from Flash.
ExternalInterface.call("sendData",tempStr);
// argument 1: javascript function, argument 2: data/variables to pass out.
 
//2. calling javascript function from Flash with recursion.
var returnValue:String = ExternalInterface.call("sendReturn",tempStr).toString();
 
//3. setting up a callback function for javascript
ExternalInterface.addCallback("callFlash",flashResponse);
// argument 1: function name called by javascript, argument 2: function on the Flash side.
// AS2 version looks like this : ExternalInterface.addCallback("callFlash",null,flashResponse);

Javascript Usage:

//1. javascript function as called from Flash.
function sendData(val){
alert(val);
document.flashForm.flashOutput.value = val;
}
 
//2. javascript function with recursion.
function sendReturn(val){
var tempData = "Hello from JS";
return tempData + ' :return';
}
 
//3. calling Flash function with javascript.
function sendToFlash(val){
flash.callFlash(val);
}



Source Here.
I was first introduced to the ColorTransform class while reading the AS2 to AS3 migration documentation about setRGB(). I was immediately impressed with the features, stomach web but intimidated when I saw the constructor and all the offset, story color and alpha arguments. I just needed to change the color of something, nothing special, no gradients, no alpha, no space shuttle landings, just a simple hex color change.

So here is a simple color change utility that is a lot closer to what I am used to.

?View Code ACTIONSCRIPT
function paint(obj:Object,color:Number):void{
var ct:ColorTransform = new ColorTransform();
ct.color = color;
obj.transform.colorTransform = ct;
}



source here
Here is an AS2 version of the SuperLoader. It works just like the AS3 version found in the Custom HTML Templates post.

Quick Use:
1. Add loaderbar.swf and mainLoader.swf to your current project directory.

2. In the html file for your current project, pilule replace the src and movie parameters to mainLoader.

3. Then add [, website like this 'flashVars', stomach 'swfURL=FlashProjectToLoad.swf'] to the load script.

Example for AC_RunActiveContent:

AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
'width', '472',
'height', '280',
'src', 'mainLoader',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
'play', 'true',
'loop', 'true',
'scale', 'showall',
'wmode', 'window',
'devicefont', 'false',
'id', 'mainLoader',
'bgcolor', '#000000',
'name', 'mainLoader',
'menu', 'true',
'allowFullScreen', 'false',
'allowScriptAccess','sameDomain',
'movie', 'mainLoader',
'salign', '',
'flashVars','swfURL=imgViewer.swf'
); //end AC code

Example for HTML Object/Embed:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="472" height="280" id="mainLoader" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="mainLoader.swf?swfURL=imgViewer.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="mainLoader.swf?swfURL=imgViewer.swf" quality="high" bgcolor="#ffffff" width="472" height="280" name="mainLoader" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

4. If your frame rate is different then 30, you will need to update the frame rate in the mainLoader.fla file.

5. If you are using this loader inside of your Flash movie, you may also need to comment out the line that sets _root to the loading file [mainMC._lockroot = true;].

Demo:

Source Here

UPDATE: 1.1

1. Added usage information, the class could be used as a simple loader monitor if needed.
2. Moved class path from com.hdi.util.HDLoader to com.hdi.as2.HDLoader.

See just the loader class below.

?View Code ACTIONSCRIPT
/**********************************************************
HDLoader is a simple loader monitor that can attach to your custom loading bar
version 1.1
date: 1-9-10
 
How To Use:
1. Have something that needs loading
 
2. Create a function to handle the loading monitor updates. Should look something like this.
function loaderUpdate(perc:Number){
// perc is the percent loaded in non decimal form. 0 - 100.
// you can make a loading bar move or scale like so
loaderBar._xscale = perc/100;
 
// you can display text of the loading status.
loaderText.text = "LOADING: " + perc + "%";
}
 
3. Create a funciton to handle when loading is complete
function loadComplete(){
// hiding the loader after loading is always a good thing.
loaderBar._visible = false;
 
// do what ever you want here, your crap is now loaded.
}
 
4. import HDLoader.
import com.hdi.as2.HDLoader;
 
5. Start the loader, placing the correct target references in the correct argument positions.
arg 1: the MovieClip doing the laoding.
arg 2: the loader update function created in step 2.
arg 3: the loader complete function created in step 3.
 
// we are assuming you have just started loading something (mc.loadMovie("someSWF.swf");
var loaderMon:HDLoader = new HDLoader(mc,loaderUpdate,loadComplete);
 
Author: Todd Williams
website: www.taterboy.com
website: hdinteractive.com
***********************************************************/
 
class com.hdi.as2.HDLoader{
var targMC:Object;
var monFunc:Function;
var finFunc:Function;
public function HDLoader(targ,mfunc,func){
init(targ,mfunc,func);
}
private function init(targ,mfunc,func){
var lbytes,tbytes,perc;
var frameParent:Object = targ._parent;
var cnt:Number = 3;
var frameObj:MovieClip = frameParent.createEmptyMovieClip("frameLdr",frameParent.getNextHighestDepth());
frameObj.onEnterFrame = function(){
lbytes = targ.getBytesLoaded();
tbytes = targ.getBytesTotal();
perc = Math.round((lbytes/tbytes)*100);
if (lbytes < tbytes) {
if(mfunc != null){
mfunc(perc);
}
}
else if(tbytes > 200){
if(cnt == 0){
if(func != undefined){
func();
}
delete frameObj.onEnterFrame;
}
cnt --;
if(mfunc != null){
mfunc(100);
}
}
}
}
}

Passing Flash variables to Javascript, online passing Javscript variables to Flash, price Calling Flash functions from Javascript.

There are a few conventions for getting Flash and Javascript to communicate like fscommand, getURL, LocalConnection, URL-encoded variables, FlashVars, SetVariable and TCallLabel to name a few.

Here is a little demo using Flash's External Interface and Javascript. It is more elegant then most of the procedures I have used in the past. Between External Interface and FlashVars, you should be able to fill most of your needs.

By the way, the syntax is almost exactly the same for AS2 and AS3.

Flash Usage:

?View Code ACTIONSCRIPT
//import flash.external.*; //for AS2
 
//1. calling javascript function from Flash.
ExternalInterface.call("sendData",tempStr);
// argument 1: javascript function, argument 2: data/variables to pass out.
 
//2. calling javascript function from Flash with recursion.
var returnValue:String = ExternalInterface.call("sendReturn",tempStr).toString();
 
//3. setting up a callback function for javascript
ExternalInterface.addCallback("callFlash",flashResponse);
// argument 1: function name called by javascript, argument 2: function on the Flash side.
// AS2 version looks like this : ExternalInterface.addCallback("callFlash",null,flashResponse);

Javascript Usage:

//1. javascript function as called from Flash.
function sendData(val){
alert(val);
document.flashForm.flashOutput.value = val;
}
 
//2. javascript function with recursion.
function sendReturn(val){
var tempData = "Hello from JS";
return tempData + ' :return';
}
 
//3. calling Flash function with javascript.
function sendToFlash(val){
flash.callFlash(val);
}



Source Here.
One of my favorite statements is try..catch..finally.

When building web applications, this we as developers need to plan for the unexpected. We know the web does not always behave at the same level of consistency for all users. This does not take into account client systems which can compound minor glitches.

There are also times that you need to compile code that is reliant on external variables. If all the conditions are not returned in a valid format AS3, being very strict, will throw an error and not complete the remaining code sequences as intended.

Though the try..catch.. statement has been around since AS1/Flash Player 7, it's value was not as profound and often overlooked as AS2 scripts would just error out quietly anyway. This was bad because bad habits were easily developed and can cause some struggles when working in the much more structured AS3 environment.

Try..catch.. is great when working with database calls, loading external media and developing an enhanced error system. The moral of this post could be "learning to take control of errors before they take control of you".

?View Code ACTIONSCRIPT
try{
randomBall(numT.text);
}
catch(e){
trace("caught error: " + e);
errT.text = "That is not a valid object name.";
}
 
// even if error occurs above or in randomBall(), the rest of the
//method still executes.
trace("end of function");

The really cool thing about try..catch.. is that it not only catches errors in the immediate script that it wraps, but from every class that is called from within the block. A word of warning, try...catch... will not protect your "for" loops from causing the "script causing Flash Player to run slowly" Error. You still need to evaluate your variables and exit number values before execution.

Below is a sample app (like Lotto) with a terrible system of user interaction, but it should demo the point.

Type in the Object name (Ball1, Ball2, Ball3, etc.) in the input area, then click Enter. The numbers on the ball are random. Try to see how many clicks it takes to get all the numbers to match. The try..catch... statement comes into play when/if a user inputs the object name incorrectly.

Demo:

source here
I was first introduced to the ColorTransform class while reading the AS2 to AS3 migration documentation about setRGB(). I was immediately impressed with the features, stomach web but intimidated when I saw the constructor and all the offset, story color and alpha arguments. I just needed to change the color of something, nothing special, no gradients, no alpha, no space shuttle landings, just a simple hex color change.

So here is a simple color change utility that is a lot closer to what I am used to.

?View Code ACTIONSCRIPT
function paint(obj:Object,color:Number):void{
var ct:ColorTransform = new ColorTransform();
ct.color = color;
obj.transform.colorTransform = ct;
}



source here
Here is an AS2 version of the SuperLoader. It works just like the AS3 version found in the Custom HTML Templates post.

Quick Use:
1. Add loaderbar.swf and mainLoader.swf to your current project directory.

2. In the html file for your current project, pilule replace the src and movie parameters to mainLoader.

3. Then add [, website like this 'flashVars', stomach 'swfURL=FlashProjectToLoad.swf'] to the load script.

Example for AC_RunActiveContent:

AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
'width', '472',
'height', '280',
'src', 'mainLoader',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
'play', 'true',
'loop', 'true',
'scale', 'showall',
'wmode', 'window',
'devicefont', 'false',
'id', 'mainLoader',
'bgcolor', '#000000',
'name', 'mainLoader',
'menu', 'true',
'allowFullScreen', 'false',
'allowScriptAccess','sameDomain',
'movie', 'mainLoader',
'salign', '',
'flashVars','swfURL=imgViewer.swf'
); //end AC code

Example for HTML Object/Embed:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="472" height="280" id="mainLoader" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="mainLoader.swf?swfURL=imgViewer.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="mainLoader.swf?swfURL=imgViewer.swf" quality="high" bgcolor="#ffffff" width="472" height="280" name="mainLoader" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

4. If your frame rate is different then 30, you will need to update the frame rate in the mainLoader.fla file.

5. If you are using this loader inside of your Flash movie, you may also need to comment out the line that sets _root to the loading file [mainMC._lockroot = true;].

Demo:

Source Here

UPDATE: 1.1

1. Added usage information, the class could be used as a simple loader monitor if needed.
2. Moved class path from com.hdi.util.HDLoader to com.hdi.as2.HDLoader.

See just the loader class below.

?View Code ACTIONSCRIPT
/**********************************************************
HDLoader is a simple loader monitor that can attach to your custom loading bar
version 1.1
date: 1-9-10
 
How To Use:
1. Have something that needs loading
 
2. Create a function to handle the loading monitor updates. Should look something like this.
function loaderUpdate(perc:Number){
// perc is the percent loaded in non decimal form. 0 - 100.
// you can make a loading bar move or scale like so
loaderBar._xscale = perc/100;
 
// you can display text of the loading status.
loaderText.text = "LOADING: " + perc + "%";
}
 
3. Create a funciton to handle when loading is complete
function loadComplete(){
// hiding the loader after loading is always a good thing.
loaderBar._visible = false;
 
// do what ever you want here, your crap is now loaded.
}
 
4. import HDLoader.
import com.hdi.as2.HDLoader;
 
5. Start the loader, placing the correct target references in the correct argument positions.
arg 1: the MovieClip doing the laoding.
arg 2: the loader update function created in step 2.
arg 3: the loader complete function created in step 3.
 
// we are assuming you have just started loading something (mc.loadMovie("someSWF.swf");
var loaderMon:HDLoader = new HDLoader(mc,loaderUpdate,loadComplete);
 
Author: Todd Williams
website: www.taterboy.com
website: hdinteractive.com
***********************************************************/
 
class com.hdi.as2.HDLoader{
var targMC:Object;
var monFunc:Function;
var finFunc:Function;
public function HDLoader(targ,mfunc,func){
init(targ,mfunc,func);
}
private function init(targ,mfunc,func){
var lbytes,tbytes,perc;
var frameParent:Object = targ._parent;
var cnt:Number = 3;
var frameObj:MovieClip = frameParent.createEmptyMovieClip("frameLdr",frameParent.getNextHighestDepth());
frameObj.onEnterFrame = function(){
lbytes = targ.getBytesLoaded();
tbytes = targ.getBytesTotal();
perc = Math.round((lbytes/tbytes)*100);
if (lbytes < tbytes) {
if(mfunc != null){
mfunc(perc);
}
}
else if(tbytes > 200){
if(cnt == 0){
if(func != undefined){
func();
}
delete frameObj.onEnterFrame;
}
cnt --;
if(mfunc != null){
mfunc(100);
}
}
}
}
}

Passing Flash variables to Javascript, online passing Javscript variables to Flash, price Calling Flash functions from Javascript.

There are a few conventions for getting Flash and Javascript to communicate like fscommand, getURL, LocalConnection, URL-encoded variables, FlashVars, SetVariable and TCallLabel to name a few.

Here is a little demo using Flash's External Interface and Javascript. It is more elegant then most of the procedures I have used in the past. Between External Interface and FlashVars, you should be able to fill most of your needs.

By the way, the syntax is almost exactly the same for AS2 and AS3.

Flash Usage:

?View Code ACTIONSCRIPT
//import flash.external.*; //for AS2
 
//1. calling javascript function from Flash.
ExternalInterface.call("sendData",tempStr);
// argument 1: javascript function, argument 2: data/variables to pass out.
 
//2. calling javascript function from Flash with recursion.
var returnValue:String = ExternalInterface.call("sendReturn",tempStr).toString();
 
//3. setting up a callback function for javascript
ExternalInterface.addCallback("callFlash",flashResponse);
// argument 1: function name called by javascript, argument 2: function on the Flash side.
// AS2 version looks like this : ExternalInterface.addCallback("callFlash",null,flashResponse);

Javascript Usage:

//1. javascript function as called from Flash.
function sendData(val){
alert(val);
document.flashForm.flashOutput.value = val;
}
 
//2. javascript function with recursion.
function sendReturn(val){
var tempData = "Hello from JS";
return tempData + ' :return';
}
 
//3. calling Flash function with javascript.
function sendToFlash(val){
flash.callFlash(val);
}



Source Here.
One of my favorite statements is try..catch..finally.

When building web applications, this we as developers need to plan for the unexpected. We know the web does not always behave at the same level of consistency for all users. This does not take into account client systems which can compound minor glitches.

There are also times that you need to compile code that is reliant on external variables. If all the conditions are not returned in a valid format AS3, being very strict, will throw an error and not complete the remaining code sequences as intended.

Though the try..catch.. statement has been around since AS1/Flash Player 7, it's value was not as profound and often overlooked as AS2 scripts would just error out quietly anyway. This was bad because bad habits were easily developed and can cause some struggles when working in the much more structured AS3 environment.

Try..catch.. is great when working with database calls, loading external media and developing an enhanced error system. The moral of this post could be "learning to take control of errors before they take control of you".

?View Code ACTIONSCRIPT
try{
randomBall(numT.text);
}
catch(e){
trace("caught error: " + e);
errT.text = "That is not a valid object name.";
}
 
// even if error occurs above or in randomBall(), the rest of the
//method still executes.
trace("end of function");

The really cool thing about try..catch.. is that it not only catches errors in the immediate script that it wraps, but from every class that is called from within the block. A word of warning, try...catch... will not protect your "for" loops from causing the "script causing Flash Player to run slowly" Error. You still need to evaluate your variables and exit number values before execution.

Below is a sample app (like Lotto) with a terrible system of user interaction, but it should demo the point.

Type in the Object name (Ball1, Ball2, Ball3, etc.) in the input area, then click Enter. The numbers on the ball are random. Try to see how many clicks it takes to get all the numbers to match. The try..catch... statement comes into play when/if a user inputs the object name incorrectly.

Demo:

source here
In normal uses PHP and other server/scripting languages are able to store variables for later usage. Due to the fact that the these pages are not actually living in a browser, adiposity remoting is one of the exceptions where variables do not live between calls. We have to plan out our Flash to PHP conversations as if PHP has very short term memory and include all the needed data to complete every transaction. For instance, page if you have to store information in a database using a userID, even though you have just recieved that userID from php, your next php call will have to include the userID because PHP just can not remember.

Here is a demo of using PHP session variables to temporarily store and read data. Even though normal variables vanish, session information is live and well.

This demo was built a while ago in AS2, but all the session code only effects the PHP side so the results should work with AS3 as well. There are some nice AS3 AMFPHP remoting tutorials already on the web. Here are a few of the many possible links: GotoAndLearn, Caleb.org

Starting php sessions are really simple.

//place this at the top of the page
session_start();
 
//use this syntax to add data to a session variable.
$_SESSION[$name] = $value;
 
//pulling data is just as straight forward.
return $_SESSION[$name];

To use the demo, type an interesting string into one of the input boxes then press "TELL". To recall the string from the session variable, press "ASK". You will see status information appear in the output areas.
Demo.

source here
I was first introduced to the ColorTransform class while reading the AS2 to AS3 migration documentation about setRGB(). I was immediately impressed with the features, stomach web but intimidated when I saw the constructor and all the offset, story color and alpha arguments. I just needed to change the color of something, nothing special, no gradients, no alpha, no space shuttle landings, just a simple hex color change.

So here is a simple color change utility that is a lot closer to what I am used to.

?View Code ACTIONSCRIPT
function paint(obj:Object,color:Number):void{
var ct:ColorTransform = new ColorTransform();
ct.color = color;
obj.transform.colorTransform = ct;
}



source here
Here is an AS2 version of the SuperLoader. It works just like the AS3 version found in the Custom HTML Templates post.

Quick Use:
1. Add loaderbar.swf and mainLoader.swf to your current project directory.

2. In the html file for your current project, pilule replace the src and movie parameters to mainLoader.

3. Then add [, website like this 'flashVars', stomach 'swfURL=FlashProjectToLoad.swf'] to the load script.

Example for AC_RunActiveContent:

AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
'width', '472',
'height', '280',
'src', 'mainLoader',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
'play', 'true',
'loop', 'true',
'scale', 'showall',
'wmode', 'window',
'devicefont', 'false',
'id', 'mainLoader',
'bgcolor', '#000000',
'name', 'mainLoader',
'menu', 'true',
'allowFullScreen', 'false',
'allowScriptAccess','sameDomain',
'movie', 'mainLoader',
'salign', '',
'flashVars','swfURL=imgViewer.swf'
); //end AC code

Example for HTML Object/Embed:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="472" height="280" id="mainLoader" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="mainLoader.swf?swfURL=imgViewer.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="mainLoader.swf?swfURL=imgViewer.swf" quality="high" bgcolor="#ffffff" width="472" height="280" name="mainLoader" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

4. If your frame rate is different then 30, you will need to update the frame rate in the mainLoader.fla file.

5. If you are using this loader inside of your Flash movie, you may also need to comment out the line that sets _root to the loading file [mainMC._lockroot = true;].

Demo:

Source Here

UPDATE: 1.1

1. Added usage information, the class could be used as a simple loader monitor if needed.
2. Moved class path from com.hdi.util.HDLoader to com.hdi.as2.HDLoader.

See just the loader class below.

?View Code ACTIONSCRIPT
/**********************************************************
HDLoader is a simple loader monitor that can attach to your custom loading bar
version 1.1
date: 1-9-10
 
How To Use:
1. Have something that needs loading
 
2. Create a function to handle the loading monitor updates. Should look something like this.
function loaderUpdate(perc:Number){
// perc is the percent loaded in non decimal form. 0 - 100.
// you can make a loading bar move or scale like so
loaderBar._xscale = perc/100;
 
// you can display text of the loading status.
loaderText.text = "LOADING: " + perc + "%";
}
 
3. Create a funciton to handle when loading is complete
function loadComplete(){
// hiding the loader after loading is always a good thing.
loaderBar._visible = false;
 
// do what ever you want here, your crap is now loaded.
}
 
4. import HDLoader.
import com.hdi.as2.HDLoader;
 
5. Start the loader, placing the correct target references in the correct argument positions.
arg 1: the MovieClip doing the laoding.
arg 2: the loader update function created in step 2.
arg 3: the loader complete function created in step 3.
 
// we are assuming you have just started loading something (mc.loadMovie("someSWF.swf");
var loaderMon:HDLoader = new HDLoader(mc,loaderUpdate,loadComplete);
 
Author: Todd Williams
website: www.taterboy.com
website: hdinteractive.com
***********************************************************/
 
class com.hdi.as2.HDLoader{
var targMC:Object;
var monFunc:Function;
var finFunc:Function;
public function HDLoader(targ,mfunc,func){
init(targ,mfunc,func);
}
private function init(targ,mfunc,func){
var lbytes,tbytes,perc;
var frameParent:Object = targ._parent;
var cnt:Number = 3;
var frameObj:MovieClip = frameParent.createEmptyMovieClip("frameLdr",frameParent.getNextHighestDepth());
frameObj.onEnterFrame = function(){
lbytes = targ.getBytesLoaded();
tbytes = targ.getBytesTotal();
perc = Math.round((lbytes/tbytes)*100);
if (lbytes < tbytes) {
if(mfunc != null){
mfunc(perc);
}
}
else if(tbytes > 200){
if(cnt == 0){
if(func != undefined){
func();
}
delete frameObj.onEnterFrame;
}
cnt --;
if(mfunc != null){
mfunc(100);
}
}
}
}
}

Passing Flash variables to Javascript, online passing Javscript variables to Flash, price Calling Flash functions from Javascript.

There are a few conventions for getting Flash and Javascript to communicate like fscommand, getURL, LocalConnection, URL-encoded variables, FlashVars, SetVariable and TCallLabel to name a few.

Here is a little demo using Flash's External Interface and Javascript. It is more elegant then most of the procedures I have used in the past. Between External Interface and FlashVars, you should be able to fill most of your needs.

By the way, the syntax is almost exactly the same for AS2 and AS3.

Flash Usage:

?View Code ACTIONSCRIPT
//import flash.external.*; //for AS2
 
//1. calling javascript function from Flash.
ExternalInterface.call("sendData",tempStr);
// argument 1: javascript function, argument 2: data/variables to pass out.
 
//2. calling javascript function from Flash with recursion.
var returnValue:String = ExternalInterface.call("sendReturn",tempStr).toString();
 
//3. setting up a callback function for javascript
ExternalInterface.addCallback("callFlash",flashResponse);
// argument 1: function name called by javascript, argument 2: function on the Flash side.
// AS2 version looks like this : ExternalInterface.addCallback("callFlash",null,flashResponse);

Javascript Usage:

//1. javascript function as called from Flash.
function sendData(val){
alert(val);
document.flashForm.flashOutput.value = val;
}
 
//2. javascript function with recursion.
function sendReturn(val){
var tempData = "Hello from JS";
return tempData + ' :return';
}
 
//3. calling Flash function with javascript.
function sendToFlash(val){
flash.callFlash(val);
}



Source Here.
One of my favorite statements is try..catch..finally.

When building web applications, this we as developers need to plan for the unexpected. We know the web does not always behave at the same level of consistency for all users. This does not take into account client systems which can compound minor glitches.

There are also times that you need to compile code that is reliant on external variables. If all the conditions are not returned in a valid format AS3, being very strict, will throw an error and not complete the remaining code sequences as intended.

Though the try..catch.. statement has been around since AS1/Flash Player 7, it's value was not as profound and often overlooked as AS2 scripts would just error out quietly anyway. This was bad because bad habits were easily developed and can cause some struggles when working in the much more structured AS3 environment.

Try..catch.. is great when working with database calls, loading external media and developing an enhanced error system. The moral of this post could be "learning to take control of errors before they take control of you".

?View Code ACTIONSCRIPT
try{
randomBall(numT.text);
}
catch(e){
trace("caught error: " + e);
errT.text = "That is not a valid object name.";
}
 
// even if error occurs above or in randomBall(), the rest of the
//method still executes.
trace("end of function");

The really cool thing about try..catch.. is that it not only catches errors in the immediate script that it wraps, but from every class that is called from within the block. A word of warning, try...catch... will not protect your "for" loops from causing the "script causing Flash Player to run slowly" Error. You still need to evaluate your variables and exit number values before execution.

Below is a sample app (like Lotto) with a terrible system of user interaction, but it should demo the point.

Type in the Object name (Ball1, Ball2, Ball3, etc.) in the input area, then click Enter. The numbers on the ball are random. Try to see how many clicks it takes to get all the numbers to match. The try..catch... statement comes into play when/if a user inputs the object name incorrectly.

Demo:

source here
In normal uses PHP and other server/scripting languages are able to store variables for later usage. Due to the fact that the these pages are not actually living in a browser, adiposity remoting is one of the exceptions where variables do not live between calls. We have to plan out our Flash to PHP conversations as if PHP has very short term memory and include all the needed data to complete every transaction. For instance, page if you have to store information in a database using a userID, even though you have just recieved that userID from php, your next php call will have to include the userID because PHP just can not remember.

Here is a demo of using PHP session variables to temporarily store and read data. Even though normal variables vanish, session information is live and well.

This demo was built a while ago in AS2, but all the session code only effects the PHP side so the results should work with AS3 as well. There are some nice AS3 AMFPHP remoting tutorials already on the web. Here are a few of the many possible links: GotoAndLearn, Caleb.org

Starting php sessions are really simple.

//place this at the top of the page
session_start();
 
//use this syntax to add data to a session variable.
$_SESSION[$name] = $value;
 
//pulling data is just as straight forward.
return $_SESSION[$name];

To use the demo, type an interesting string into one of the input boxes then press "TELL". To recall the string from the session variable, press "ASK". You will see status information appear in the output areas.
Demo.

source here
I know there are some really nice scroll components similar to this on the web, viagra 100mg but I think this still has merit. It seems like every job comes with a different scroll bar design, information pills the buttons and scroll bar can be any shape, size or configuration. While most Flash scroll bar components are easily skinned, this is kind of the opposite of skinning. You create the artwork, convert each item to a MovieClip, then apply the class. The code wraps the artwork instead of the artwork skinning the code.

This class started around 3 years ago and has been modified for many different projects and continued to evolve into what we have today. It is not perfect, but it can handle most jobs needing custom scroll bars.

Demo:

Some of the features include:
Auto On/Off - you may need a scrolling clip that could change in size as dynamic content is displayed or entered. When the scroll bar is not needed, it will turn itself off and back on when it is needed. Or you can manually turn it on and with your code.

Easing - Control the amount of ease or delay of the scrolling object.

Scroll Bar Resizing - To simulate the function of desktop applications, the scroll bar will automatically scale depending on the amount of scroll, This feature can be turned on or off.

Auto Resize - Say you have a project that needs multiple scroll bars. One 150 px tall, the other 500 px tall. With this class you can build one set of artwork and the class will automatically adjust the bottom button, scroll background, scrollbar scroll area to fit the needed size. Based on Mask size.

Down Button Flip - This will not change the world, but if you have an Up Button that is exactly like the bottom button just flipped vertically. The class will compensate for this, saving a few minutes of development time and a few kilobytes.

Scroll Background Hiding and resizing - Many scroll bars have a background track, you may want it visible when the scroll bars are disabled as a visual place holder. This feature can be turned on or off.

Works without a scroll bar or without up and down buttons.

Button Type Behaviors - Even though the buttons and scrollbars are made with movieClips, sample code and calls are provided to give them button type effects on rollOver, rollOut, press and release.

Usage may seem a little complex at first, but if you like designing and using custom scroll bars and buttons, it is totally worth it.

1. Create all your scroll objects, they should be converted to MovieClips.
(upButton, DownButton, ScrollBar, and ScrollBkg)

2. Place the up and down buttons, scroll bar, and scroll background into a new movieClip.
(ScrollObject)

3. Align the top button and scrollbar to it's starting position.

4. Create the MovieClip to be scrolled, can include a TextField, graphics or both.
(ScrollMovieClip)

5. Create a mask and convert it to a MovieClip and mask the viewable area of the "ScrollMovieClip".
(MaskMC)

5. Apply the class, this is the most complex part.

var scrollVert:HDScrollMC = HDScrollMC(ScrollObject, ScrollMovieClip, MaskMC, UpButton, DownButton, ScrollBar, ScrollBarResize, ScrollBkg, ScrollBkgHide, DownButtonFlip, ResizeScrollObjects, AutoOn, Ease);

This is where you assign all the MovieClips and enable any of the features. Other than the 7 MovieClips that the class points to, the other 6 arguments are features to enable or disable.

ScrollBarResize: Boolean (true/false)
ScrollBkgHide: Boolean (true/false)
DownButtonFlip: Boolean (true/false)
ResizeScrollObjects: Boolean (true/false)
AutoOn: Boolean (true/false)
Ease: Number (0-100) *(5 - 20) for best effect.

Once enabled, it can manage itself or you can manage it manually with a method like so:
scrollVert.ScrollStatus(true); or scrollVert.ScrollStatus(false);

When you are done with everything, you can strip all the code out by calling scrollVert.killScrollBar();

You can check the status of the scrollbar at anytime using
scrollVert.scrollStat; returns true or false.

?View Code ACTIONSCRIPT
import mx.utils.Delegate;
class com.hdi.as2.HDScrollMC{
/*
HDScrollMC oringinally created 11/2005 by Todd Williams and HD Interactive.
©2005 - 2010 HD Interactive & Taterboy, Inc.
version: 2.4
Date: 7-26-10
 
Compatibility:  Action Script 2, Flash Player 7 and higher.
Purpose:
To Build a scrollbar class that is truly portable and can be applied to custom artwork
of any size and shape. This class wraps the graphics instead of graphics wrapping the code.
This gives the designer complete 	control over look and feel. One of the major features
needed was to have one set of scroll objects to be 	used in multiple instances and allow
them to automatically conform to different scrolling needs. One set of scroll objects can
control a scroll window that is 200 pixels tall or 600 pixels tall on the same stage at the
same time.
 
HDScrollMC handles Vertical Scrolling Only, for horizontal scrolling use HDScrollHorizontal.
 
Features:
1. Automatically arranges scroll objects so that one set scrollbar objects can be used in multiple
instances with different sized scrolling windows.
2. Resizing scroll bar similar to OS and desktop application scroll bar behavior.
3. Customizable Easing options for enhanced feel and response.
4. Code Wraps to custom artwork instead of skinning a preexisting component.
5. Auto on and off. The scroll bar is smart enough to know when it is needed or not.
6. Code provided to build button type behavior into MovieClip buttons and scrollbar.
7. Have multiple scrolling objects visible and functioning on the same stage at a time.
 
 
Preparation:
To prepare graphic objects for use with HDScrollMC, you will need to create 7 MovieClips.
MC 1. The MovieClip to be scrolled:
For scrolling text, create a TextField inside of a MovieClip, fill it with text manually,
or use dynamic text and the TextField.autoSize = true;
 
MC 2. Mask for MC 1 display area: This must be a MovieClip so that class can determine the size of the
masked area.
 
MC 3. Container for Scroll Objects: To make it possible to have multiple scrolling objects on the
stage at the same time, it is best to have at least the scrollbar and scroll button objects in a
container, the mask and MC to be scrolled can also be in this container if desired.
 
MC 4 & 5.  Scroll buttons UP and Down. These buttons are not required if a scrollbar only design is
needed. Scroll buttons can be MovieClips or buttons. Code for rollover and press events will be
sent to each MovieClip. If you want to use a MovieClip, disperse your button states on frames as you
would a button and add a stop action to the first frame. Also the down button and be a vertically
flipped instance if the top button if the design allows for this.
 
The scroll button MovieClips receive the following method calls from the class.
btnEvent(1); roll on
btnEvent(0); roll out
btnEvent(2); mouse down
btnEvent(1); mouse up
 
Use the sample method below for MovieClips with button type behavior.
**Super Simple Sample:
function btnEvent(val){
gotoAndStop(val+1);
}
 
Long-hand Sample:
function btnEvent(val){
if(val == 1){
//roll on / release
}
else if(val == 2){
//press
}
else{
//roll out
}
}
 
MC 6. Scroll Bar: Must be a MovieClip and like the buttons, code will be sent for rollover and press events.
A scrollbar is not required if a button only design is required. The scroll bar has a resize feature
with two modes. The scroll bar will resize based on the amount of scroll and the size of the scroll
area, similar to OS scroll bars.
 
Mode 1: (recommended) The scrollbar MovieClip resizes the whole Scroll Bar MovieClip. Some graphic distortion or stretching may occur.
You can set the scale9Grid to this MovieClip prior to enabling this class.
 
Mode 2: Three individual MovieClips within the Scroll Bar are used to represent the top, middle
and bottom of the scroll bar, the middle will be stretched the top and bottom MovieClips will retain
their graphic integrity. To use this option divide the Scrollbar into three horizontal slices. Create
MovieClip instances named scrollbBarTop, scrollBarBot, and scrollBarMid.
(this was implimented before scale9Grid capibilities (Flash Player 7))
 
The scrollbar receives the following method calls from the class.
barEvent(1); roll on
barEvent(0); roll out
barEvent(2); mouse down
barEvent(1); mouse up
 
Use the method below for MovieClips with button type Behavior.
**Super Simple Sample:
function btnEvent(val){
gotoAndStop(val+1);
}
 
Long-hand Sample:
function barEvent(val){
if(val == 1){
//roll on / release
}
else if(val == 2){
//press
}
else{
//roll out
}
}
 
MC 7. Scroll background, Must be a MovieClip,  will span the entire size of the scroll area
if the class is set to resize the objects. This MovieClip can be used to have a decorative
track for the scroll area and can be set to vanish or remain when the scrollbar is disabled
or enabled. This object is not required.
 
Usage:
Place the class folder in the working directory next to the fla you are using;
add import com.hdi.as2.HDScrollMC; to the actions panel before the code below.
 
var vertScroll:HDScrollMC = new HDScrollMC(ScrollObject, ScrollMovieClip, MaskMC, UpButton,
DownButton, ScrollBar, ScrollBarResize, ScrollBkg, ScrollBkgHide, DownButtonFlip ResizeScrollObjects,
AutoOn, Ease);
 
**should be all one line.
 
I know there are a lot of arguments, and the descriptions/Usage are as follows:
1. ScrollObject - (required) target of scroll object container, MC 3 from the preparation list.
2. ScrollMovieClip - (required) target of MovieClip to be scrolled, MC 1 from preparation list.
3. MaskMC - (required) target to Mask for the scrolling MovieClip, MC 2 from preparation list.
4. UpButton - target or null, to Up Button MovieClip or Button. MC 4 & 5 from preparation list.
5. DownButton - target  or null, to Down Button MovieClip or Button. MC 4 & 5 from preparation list.
6. ScrollBar - target or null, to Scroll Bar MovieClip. MC 6 from preparation list.
7. ScrollBarResize - Boolean: true or false, To enable scroll bar resizing depending on scroll amount.
8. ScrollBkg - target or null, to resize scroll background graphic.
9. ScrollBkgHide - Boolean: true or false, default behavior is to hide scroll background and scroll objects
when scrollbar is disabled;
10. DownButtonFlip - Boolean: true or false, If the down button is an instance of the up button that is
vertically flipped.
11. ResizeScrollObjects - Boolean: true or false, when scroll bar loads, it calculates the size of the mask,
buttons, scrollbars and distributes them to the appropriate height and coordinates. This allows
different instances of a set of scroll objects to be used with different sized scrolling Objects.
12. AutoOn - Boolean: true or false, Turns control of turning the scroll bar objects on and off to the class.
If the scrolling MovieClip changes in size the class will automatically enable or disable the
scroll objects.
13. Ease - Number, changes the scrolling MovieClip's response to scroll events. set to 0 for no delay.
 
 
Other methods and properties:
You can use these properties and methods for external control and monitoring.
 
scrollStatus:  property (Boolean) - tells if scrolling is enabled or disabled.
setScrollStatus: Function (Boolean)- manually enables/disables scrolling - use with AutoOn set to false.
killScrollBar: Function - permanently disables the scrollbar and removes all listeners and events.
setScrollPos: Function (Number) manually sets the scroll position of the scrolling clip and uptdates the scrollbar position.
 
Tips:
1. For best performance, zero out  x and y coordinates to whole numbers on all MovieClips.
Also try to build artwork with artwork rounded to the nearest whole pixel. This stops a lot
of the little glitches 	that can appear when a user interacts with the scrolling objects.
 
2. If you do not want your scroll objects to flash on then off on load, set the scroll Object
Container's (MC 3) 	alpha property to 0. The class will automatically set it to 100 when enabled.
 
3. By default the class uses the scroll background  and/or the up button to align and distribute
the rest of the objects. You should align the top button and the top of the scroll background.
The top of the scrollbar should be aligned to the button of the top button. These coordinates
are used as starting points for all of 	the scroll objects and will used as return positions
when the scroll bar is reset.
 
*/
private var scrollObj:Object; //scroll MovieClip containing up button, down button, scrollbar, background.
private var tClip:Object; //MovieClip that holds the text clip.
private var tMask:Object; //Mask for the Text MovieClip, must be a MovieClip.
private var dwnBtn:Object; //Down button, should be movieClip or button.
private var upBtn:Object; //Down button, should be movieClip or button.
private var scrollBar:Object; //MovieClip to do the scrolling.
private var barAutoSize:Boolean; //Resizes the scroll bar to the scroll amount.
private var scrollBkg:Object; //Background behind scrollButtons and scrollBar.
private var scrollBkgHide:Boolean; //When scroll objects are not on, ScrollBkg is can be visible or not.
private var flipDwnButton:Boolean; // If you have flipped the upBtn to make the dwnBtn. Used for autoResize.
private var resizeObj:Boolean; // Resizes scroll objects to match the mask size and location.
private var autoOn:Boolean; //Let's the scroll object control if the scrollBar is on or off, depending on the size of the text box.
private var easeIn:Number; // Amount of delay for scrolling text to ease into possition.
private var scrollYPos:Number;
public var scrollStatus:Boolean; //Scrollbar is On or Off.
private var startParams:Boolean;
private var inc:Number;
private var resetAllSizes:Function;
function HDScrollMC(targObj,tclip,tmask,upbtn,dwnbtn,sbar,barsize,sbkg,sbkgHide,flip,reSize,auto,ease){
scrollObj = targObj;
tClip = tclip;
tMask = tmask;
upBtn = upbtn;
dwnBtn = dwnbtn;
scrollBar = sbar;
barAutoSize = barsize;
scrollBkg = sbkg;
scrollBkgHide = sbkgHide;
flipDwnButton = flip;
resizeObj = reSize;
autoOn = auto;
easeIn = ease;
scrollYPos = tClip._y;
if(tMask._height >= tClip._height){
scrollStatus = false;
if(scrollBkgHide && scrollBkg){
scrollBkg._visible = false;
}
 
}
else{
scrollStatus = true;
}
var testConfig:Boolean = true;
if(scrollObj == null || scrollObj == undefined){
trace("ERROR: ScrollObject (arg 0) needs to be a valid MovieClip ");
testConfig = false;
}
if(tMask == null || tMask == undefined){
trace("ERROR: MaskMC (arg 2) needs to be a valid MovieClip - please mask the scrolling object with a named instance of a MovieClip");
testConfig = false;
}
if(easeIn == NaN || easeIn == undefined){
trace("ERROR: Ease (arg 12) needs to be a Number - 0 for no ease, 1 and higher to add ease/delay to the scrolling object");
testConfig = false;
}
if((scrollBar == null || scrollBar == undefined) && (upBtn == null || upBtn == undefined) && (dwnBtn == null || dwnBtn == undefined)){
trace("ERROR: Very funny, you need at least 1 control object (scrollBar, upBtn or dwnBtn) assigned to initiate scrolling");
testConfig = false;
}
if(testConfig){
init();
}
}
public function setScrollStatus(val){
scrollStatus = val;
scrollObj.stat = val;
scrollObj.tmask = tMask;
scrollObj.yPos = scrollObj.starty;
tClip._y = scrollObj.starty;
scrollBar._y = scrollObj.bary;
setSizes();
if(val){
upBtn._visible = true;
dwnBtn._visible = true;
scrollBar._visible = true;
scrollBkg._visible = true;
}
else{
upBtn._visible = false;
dwnBtn._visible = false;
scrollBar._visible = false;
if(scrollBkgHide){
scrollBkg._visible = false;
}
}
}
public function setScrollPos(val){
setSizes();
var tempY:Number = val;
if(tempY > scrollObj.starty){
tempY = scrollObj.starty;
}
else if(tempY < scrollObj.starty - scrollObj.scrolldist){
tempY = scrollObj.starty - scrollObj.scrolldist;
}
 
scrollObj.yPos = tempY
tClip._y = tempY;
scrollBar._y = (((scrollObj.starty - scrollObj.yPos) / scrollObj.scrolldist) * scrollObj.barslack) + scrollObj.topy;
}
public function killScrollBar(){
tClip._y = scrollObj.starty;
scrollBar._y = scrollObj.bary;
scrollObj.yPos = scrollObj.starty;
upBtn._visible = false;
dwnBtn._visible = false;
scrollBar._visible = false;
delete scrollObj.onEnterFrame;
delete scrollBar.onMouseUp;
delete scrollBar.onMouseDown;
delete scrollBar.onMouseMove;
delete upBtn.onMouseUp;
delete upBtn.onMouseDown;
delete dwnBtn.onMouseUp;
delete dwnBtn.onMouseDown;
delete upBtn.onRollOut;
delete upBtn.onRollOver;
delete dwnBtn.onRollOut;
delete dwnBtn.onRollOver;
delete scrollBar.onRollOut;
delete scrollBar.onRollOver;
clearInterval(inc);
Mouse.removeListener(scrollObj.myWheel);
}
private function setSizes(){
scrollObj.maskh = tMask._height;
if(tClip._y < scrollObj.starty - (tClip._height - scrollObj.maskh)){
if(tClip._height <= scrollObj.maskh){
scrollObj.yPos = scrollObj.starty;
}
else{
scrollObj.yPos = scrollObj.starty - (tClip._height - scrollObj.maskh);
}
}
else if(tClip._y > scrollObj.starty){
scrollObj.yPos = scrollObj.starty;
}
 
tClip._y = scrollObj.yPos;
 
if(barAutoSize == true){
//setup dynamic scrollbar height.
var sbHeight:Number;
if(tClip._height > scrollObj.maskh){
sbHeight = Math.floor((scrollObj.maskh / Math.ceil(tClip._height)) * scrollObj.bardist);
}
else{
//sbHeight =
}
if(sbHeight < 20){
sbHeight = 20;
}
if(scrollBar.scrollbBarTop._name == undefined){
 
scrollBar._height = sbHeight;
}
else{
scrollBar.sheight = sbHeight
scrollBar.scrollBarMid._height = scrollBar.sheight - (scrollBar.scrollbBarTop._height + scrollBar.scrollbBarTop._height);
scrollBar.scrollbBarTop._y = scrollBar.scrollBarMid._height + scrollBar.scrollBarMid._y;
}
 
}
scrollObj.bht = Math.ceil(scrollBar._height);
scrollObj.barslack = (scrollObj.boty - scrollObj.topy) - scrollObj.bht;
scrollObj.scrolldist = Math.ceil(Math.ceil(tClip._height) - (scrollObj.maskh));// + 10;
scrollObj.scrollspeed = scrollObj.scrolldist / 8;
scrollObj.yspeed = tMask._height/10;//16;
scrollObj.upscroll = 0;
scrollObj.dwnscroll = 0;
scrollBar._y = (((scrollObj.starty - scrollObj.yPos) / scrollObj.scrolldist) * scrollObj.barslack) + scrollObj.topy;
}
private function setCoors(){
tMask._height = Math.ceil(tMask._height);
scrollObj.maskh = tMask._height;
if(resizeObj){
flipDwnButton == true? dwnBtn._y = Math.round(tMask._height): dwnBtn._y = Math.round((tMask._height) - dwnBtn._height);
//flipDwnButton == true? dwnBtn._y = Math.round(tMask._height + tMask._y): dwnBtn._y = Math.round((tMask._height + tMask._y) - dwnBtn._height);
 
if(scrollBkg != null){
upBtn != null ? scrollBkg._height = scrollObj.maskh: scrollBkg._height = scrollObj.maskh;
}
 
}
upBtn._y = Math.floor(upBtn._y);
scrollObj.tht = Math.ceil(tClip._height);
if(!startParams){
scrollObj.starty = tClip._y;
startParams = true;
}
//scrollObj.starty = tClip._y;
scrollObj.barx = scrollBar._x;
scrollObj.bary = scrollBar._y;
if(upBtn != null){
scrollObj.topy = Math.round(upBtn._y + upBtn._height);
flipDwnButton == true? scrollObj.boty = Math.round(dwnBtn._y - dwnBtn._height): scrollObj.boty = Math.round(dwnBtn._y);
}
else{
scrollObj.topy = Math.round(scrollBar._y);
scrollObj.boty = Math.round(scrollBar._y + tMask._height);
}
scrollObj.bardist = (scrollObj.boty - scrollObj.topy);
setSizes();
}
 
private function init(){
setCoors();
if(scrollStatus == false){
upBtn._visible = false;
dwnBtn._visible = false;
scrollBar._visible = false;
}
else{
upBtn._visible = true;
dwnBtn._visible = true;
scrollBar._visible = true;
}
scrollObj._alpha = 100;
scrollObj.objectRef = this;
scrollBar.objectRef = this;
upBtn.objectRef = this;
dwnBtn.objectRef = this;
var scrollParent:Object = scrollObj;
 
scrollObj.stat = scrollStatus;
scrollObj.yPos = scrollYPos;
 
var ref:Object = this;
scrollObj.myWheel = new Object();
scrollObj.myWheel.onMouseWheel = function(delta){
trace(delta);
 
if(ref.scrollObj.stat && ref.scrollObj.tmask.hitTest(_root._xmouse, _root._ymouse)){
if(delta > 0){
if(ref.scrollObj.yPos < ref.scrollObj.starty - ref.scrollObj.yspeed){
ref.scrollObj.yPos += Math.round(ref.scrollObj.yspeed);
ref.scrollBar._y = (((ref.scrollObj.starty - ref.scrollObj.yPos) / ref.scrollObj.scrolldist) * ref.scrollObj.barslack) + ref.scrollObj.topy;
}
else{
ref.scrollObj.yPos = ref.scrollObj.starty;
ref.scrollBar._y = ref.scrollObj.topy;
}
}
else if(delta < 0){
if(ref.scrollObj.yPos > ref.scrollObj.starty - (ref.scrollObj.scrolldist - ref.scrollObj.yspeed)){
ref.scrollObj.yPos -= Math.round(ref.scrollObj.yspeed);
ref.scrollBar._y = (((ref.scrollObj.starty - ref.scrollObj.yPos) / ref.scrollObj.scrolldist) * ref.scrollObj.barslack) + ref.scrollObj.topy;
}
else{
ref.scrollObj.yPos = ref.scrollObj.starty - ref.scrollObj.scrolldist;
ref.scrollBar._y = ref.scrollObj.boty - ref.scrollObj.bht;
}
}
}
}
Mouse.addListener(scrollObj.myWheel);
 
if(resizeObj){
resetAllSizes = setCoors;//setSizes;
}
 
scrollBar.onMouseDown = function(){
if(this._visible == true){
if(this.hitTest(_root._xmouse, _root._ymouse, false)){
this.barEvent(2);
scrollParent.scrollchk = true;
this.startDrag(false,scrollParent.barx,scrollParent.topy,scrollParent.barx,scrollParent.boty - scrollParent.bht);
this.onMouseMove = function(){
if(scrollParent.scrollchk == true){
scrollParent.yPos = scrollParent.starty - (scrollParent.scrolldist * ((this._y - scrollParent.topy)  / scrollParent.barslack));
}
 
}
}
}
else{
}
}
scrollBar.onMouseUp = function(){
scrollParent.scrollchk = false;
if(this.hitTest(_root._xmouse, _root._ymouse, false)){
this.barEvent(1);
}
this.stopDrag();
}
scrollBar.onRollOut = function(){
this.barEvent(0);
}
scrollBar.onRollOver = function(){
this.barEvent(1);
}
upBtn.onMouseDown = function(){
if(this._visible == true){
if(this.hitTest(_root._xmouse, _root._ymouse, false)){
this.btnEvent(2);
scrollParent.upscroll = 1;
}
}
}
upBtn.onMouseUp = function(){
if(this.hitTest(_root._xmouse, _root._ymouse, false)){
this.btnEvent(1);
}
else{
this.btnEvent(0);
}
scrollParent.upscroll = 0;
}
upBtn.onRollOut = function(){
this.btnEvent(0);
}
upBtn.onRollOver = function(){
this.btnEvent(1);
}
dwnBtn.onMouseDown = function(){
if(this._visible == true){
if(this.hitTest(_root._xmouse, _root._ymouse, false)){
this.btnEvent(2);
scrollParent.dwnscroll = 1;
}
}
}
dwnBtn.onMouseUp = function(){
if(this.hitTest(_root._xmouse, _root._ymouse, false)){
this.btnEvent(1);
}
else{
this.btnEvent(0);
}
scrollParent.dwnscroll = 0;
}
dwnBtn.onRollOut = function(){
this.btnEvent(0);
}
dwnBtn.onRollOver = function(){
this.btnEvent(1);
}
 
inc = setInterval(Delegate.create(this, frameLoop),10);
}
 
private function frameLoop(){
if(scrollObj.upscroll == 1){
if(scrollObj.yPos < scrollObj.starty - scrollObj.yspeed){
scrollObj.yPos += Math.round(scrollObj.yspeed);
scrollBar._y = (((scrollObj.starty - scrollObj.yPos) / scrollObj.scrolldist) * scrollObj.barslack) + scrollObj.topy;
}
else{
scrollObj.yPos = scrollObj.starty;
scrollBar._y = scrollObj.topy;
}
}
if(scrollObj.dwnscroll == 1){
if(scrollObj.yPos > scrollObj.starty - (scrollObj.scrolldist - scrollObj.yspeed)){
scrollObj.yPos -= Math.round(scrollObj.yspeed);
scrollBar._y = (((scrollObj.starty - scrollObj.yPos) / scrollObj.scrolldist) * scrollObj.barslack) + scrollObj.topy;
}
else{
scrollObj.yPos = scrollObj.starty - scrollObj.scrolldist;
scrollBar._y = scrollObj.boty - scrollObj.bht;
}
}
 
if(scrollStatus){
var dirStat:Number = 0;
scrollObj.dwnscroll == 1 ? dirStat = -1:null;
scrollObj.upscroll == 1 ? dirStat = 1:null;
easeMainClip(tClip,scrollObj.yPos,easeIn,dirStat);
if(autoOn){
if(tClip._height <= tMask._height){
setScrollStatus(false);
}
var checkScrollDist:Number = Math.ceil(Math.ceil(tClip._height) - (scrollObj.maskh));// + 10;
if(scrollObj.scrolldist < checkScrollDist || (scrollObj.maskh != tMask._height)){
//reset size - ;
if(resetAllSizes && tMask._height != scrollObj.maskh){
resetAllSizes();
}
setSizes();
}
else if (scrollObj.scrolldist > checkScrollDist){
//reset size + ;
if(resetAllSizes && tMask._height != scrollObj.maskh){
resetAllSizes();
}
setSizes();
}
}
 
}
else{
if(autoOn){
if(tClip._height > tMask._height){
setScrollStatus(true);
}
resetAllSizes();
}
}
updateAfterEvent();
}
private function easeMainClip(obj,val,ease,dir){
if(ease > 0){
if(dir == 0){
if(Math.floor(obj._y) > val + 1){
dir = -1;
}
else if(Math.ceil(obj._y) < val - 1){
dir = 1;
}
}
if(dir == -1){
obj._y -= (obj._y - val)/ease;
}
else if(dir == 1){
obj._y += (val - obj._y)/ease;
}
else{
obj._y = val;
}
}
else{
obj._y = val;
}
}
 
}

Source Here

***** Some of the applications of this class may be better suited for HDSlide, a newer AS2 class to make things slide horizontally or vertically, without scroll bars using mouse movement.

Major Update and rewrite (2.0), Please download again to update your files

1. Multiple instances now work
2. Scrollbar background will be invisible if the text is not the correct size.
3. Added in code for resizing the text MC and mask to be more browser like. Not fully tested but does not seem to break other features.
4. Added code for scroll wheel support, but is it commented out. I have not tested it yet with multiple instances.

Horizontal version (2.2) is is available here.
Horizontal Scroll Source

UPDATE: 2.1

Source can be updated, using the links above.
added - clearInterval(inc); to the killScrollBar method.

UPDATE: 2.2

Source can be updated, using the links above.
1. Minor bug fixes adding reliability.
2. Scrollbars adjust if content is resized when autoOn and ResizeScrollObjects are set to true, like browser window functionality.
3. Horizontal scroll bar has been fully tested to work with vertical scroll bar on the same ScrollMovieClip. (browser type functionality)
4. Some of the documentation and usage in the class files has been clarified.
5. Both class files are included in each download above (horizontal and vertical).

UPDATE: 2.3

1. MouseWheel is now enabled by default. Thanks Pawe for pointing out the ref reference. MouseWheel support in AS2 still only works on Windows, not my fault, that is a dispute with Flash Player and Apple.
2. Added setScrollPos(Number) function to manually set the scroll position of the scrolling clip and uptdate the scrollbar position. In my testing calling this right after a textField autoSize call did not work until the next frame. Giving the scrolling clip a chance to redraw. Just pass in a number of where you want the scrolling clip to go. If the number is out of range, it will automatically correct itself.
3. Moved the class path from com.hdi.util.HDScrollMC to com.hdi.as2.HDScrollMC. All previous and future asctionScript 2 classes with organized this way.

UPDATE: 2.4

1. Fixed an issue when overriding scrollMC postition after changing the size of the scrollMC.

Thanks for the input and testing.

The Tampa Flash, physician stuff Flex and AIR Developers Group has launched their website with their first meeting scheduled for October 7th, check 2008 at 7:00 PM. They have some cool things planned and some great sponsors. The meeting will be held at the Art Institutes of Tampa and refreshments provided by the International Culinary Schools.

Greg Wilson, from Adobe, will be the presenter for the inaugural event and we can expect frequent visits from Adobe representatives in the future. Find out more at www.tffadg.com and RSVP if you are in the area.

Technorati Tags: , , , , ,

Leave a Reply