Archive for the ‘Flash Tutorials’ Category

25
Jul

A quick way to detect Idleing in flash actionscript 2

Posted By wassim in Flash, Flash Tutorials

So I had this customer who asked me to do an flv player for his site, he wanted the controls of the player to fade out when the user is idle, now flash does not have an onIdle event of any kind and the only way around this was to trigger a signal when the mouse kept its position (did not move) for more than 3 second (or x seconds, customizable) or keys has not been pressed for x seconds.

Again, flash does not have an onMouseStill event so I had to do it this way:


this.onMouseMove=function(){
clearTimeout(toInt);
fadeControlsIn();
toInt=setTimeout(fadeOut,3000);

}
function fadeOut(){
//fading out code goes here...
}
function fadeControlsIn(){
/fading in code goes here....

}

A similar approach can be applied to keypresses.

Hope this helps.

25
Apr

Left trim and right trim actionScript functions

Posted By wassim in Flash, Flash Tutorials

Left trim and right trim actionScript functions
These useful functions (lTrim and rTrim) are available in almost all programming languages yet not in actionScript, so here’s how to write ones yourself:
lTrim is easier, so we’ll start with that:

Actionscript:
  1. function lTrim(txt:String):String {
  2.     while
  3.             (txt.charAt(0)==" "
  4.             ||txt.charAt(0)=="\t"
  5.             || txt.charAt(0)=="\n"
  6.             || txt.charAt(0)=="\r"
  7.             ) {
  8.         txt=txt.substring(1,txt.length-1);
  9.     }
  10.     return txt;
  11. }
  12. //usage example:
  13. trace(lTrim("   here's a text to trim"));// outputs : here's a text to trim

Pretty much straight forward, the function accepts a string as argument and returns a string, it checks whether the first character is a space, tab, or newline character and simply cuts it out and runs the same check on the resulting substring until there are no white spaces left at the leftmost end of the string.

Now for the slightly harder rTrim function,

Actionscript:
  1. function rTrim (txt:String):String {
  2.    
  3.     while (txt.charAt(txt.length-1)==" " ||txt.charAt(txt.length-1)=="\t" || txt.charAt(txt.length-1)=="\n" || txt.charAt(txt.length-1)=="\r"){
  4.         txt=txt.substring(0,txt.length-1);
  5.     }
  6.     return txt;
  7.    
  8. }
  9. trace(rTrim ("rtrim this please  \n\r       ")+"delimiter");//outputs : rtrim this pleasedelimiter

The logic is very similar, feel free to use these wherever you like.
I would recommend using these as part of a class for string manipulation.
Btw, these functions are AS2 and AS3 compatible.

23
Feb

actionScript AS2 (typewriter) text effect

Posted By wassim in Flash, Flash Tutorials

I needed a typewriter text effect with a twist for an mp3 player I was making, looked around and could not find one that suits my needs so I wrote this class, should do the job.

Actionscript:
  1. /*
  2. @author = Wassim Sidani
  3. @www.sidani.info
  4. usage example:
  5. var tE:TxtEff=new TxtEff(textFieldInstance, textToWrite, delay);
  6. textFieldInstance= a reference to a text field.
  7. textToWrite= the actual text that will be typed in the text field.
  8. delay = dealy in milliseconds before writing next character;
  9. */
  10. class TxtEff extends TextField
  11. {
  12.     function TxtEff (me, txt, delay)
  13.     {
  14.         //clearInterval(myInt);
  15.         //clearTimeout(myOtherIn);
  16.         myClass=this;
  17.         inite (me, txt, delay);
  18.     }
  19.     function inite (me, txt, delay)
  20.     {
  21.        
  22.         var j : Number = 0;
  23.         chars = ['$', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', ' '];
  24.         var initialText : String = txt;
  25.         for (k = 0; k <txt.length; k ++)
  26.         {
  27.             me.replaceText (k, k + 1, '-');
  28.         }
  29.         var myInt : Number = setInterval (go, delay, me);
  30.         function go (me)
  31.         {
  32.             //trace('goo is called and me.text ='+me.text);
  33.             if (j <initialText.length)
  34.             {
  35.                 //trace('j is ' + j+" and chars.length is "+chars.length);
  36.                 for (var i = 0; i <chars.length; i ++)
  37.                 {
  38.                     if (initialText.charAt (j) == chars [i])
  39.                     {
  40.                         me.replaceText (j, j + 1, chars [i]);
  41.                         j ++;
  42.                         break;
  43.                     }
  44.                 }
  45.             } else
  46.             {
  47.                 clearInterval (myInt);
  48.                 trace ("done j=" + j);
  49.                 //myOtherIn=setTimeout(myClass.inite,3000,me,txt,delay);
  50.             }
  51.         }
  52.     }
  53.    
  54. }

23
Dec

Flash scrollPane component scrollbar not showing up

Posted By wassim in Flash, Flash Tutorials

This fix applies to flash 8 scrollPane component v2.

Here's the problem: you attach content dynamically at runtime to your scrollPane, then much to your surprise, the vertical scroll bar doesn't show up despite the fact that you specified vScrollPolicy as "on" or "auto" and that the content is larger than the scrollPane.

You probably know the problem that's why you are here! Google dropped you in the right place, I have found a solution! A working fix.

After searching for two hours, and trying all the suggested workarounds like calling the redraw() method on the instance or the onComplete(), doLater() or even the invalidate() method, none of these worked, I even tried lowering the frame rate of the movie but all this ended up failing so I started experimenting and I found the following:

If you change the position of the content clip inside your scroll pane component, the scroll bar will fail to account for this change, it might even not appear in the first place, if you add content dynamically, like attaching a movieClip from the library on runtime, you will also face issues with the scroll bar.

What I did to solve the issue and debug is that I simply called the setSize() method twice, in the first call, I assigned a height higher than the desired one then in the second call, I assigned the desired height and this worked ! the scroll bar of the scrollpane now appears and behaves as expected.

Note that this fixed the case where the content is being attached from the library, not loaded externally.

 

4
Dec

Flash Player cache and smaller swf files

Posted By wassim in Flash Tutorials

This new feature has been introduced in flash player 9,0,115,0.

 Certain standard flash components (dataGrid, comboBox, list etc..) are used by a large number of websites. For example, More than 100 Kb of identical data has to be downloaded for every Flex application (the flex framework) . With the new Flash Player cache, Flex applications will download and start up much faster because users will probably have the framework already in a special cache from one site and will not need to download it again.

The Flash Player cache is not the same as the browser's cache. It works by storing the contents of a new type of file (SWZ) on your hard drive. When a website makes a request for the SWZ file from the server, Flash Player first checks to see whether it has the matching file already in the cache before attempting to download it.

 Besides providing smaller size advantage and thus faster loading, this feature is also beneficial to the application host by reducing bandwidth.