Adobe Flash

When 2 – 1 = 2 when doing math operations with dates in ActionScript

   Let’s say we have the following simple use-case: we have to create a calendar with Month view. The Month view can start on any day, it can be 1st of March, it can be 15th of March, it can be even 31th of March.

   So, assume that our current date range starts with 31th of March. And now, what if we want to scroll the date back one month? Hah,easy, you would jump immediately with straightforward solution, just subtract 1 month from current Date.

   Writing a few lines of code, doing some traces….

var startDate : Date = new Date (2011, 2, 31);
var newDate : Date = new Date (startDate.time); //displays Thu Mar 31 2011
 
trace ("startDate=", startDate.toDateString()); 
newDate.month--;		
 
trace ("newDate=", newDate.toDateString()); //displays Thu Mar 3 2011

..and wait a minute, how it is possible that 2 – 1 = 2 for new month value?

   Well, the issue is that Flash Player behind the scene is fixing invalid resulting date for February month. We could not have February date with 31 days, so somebody has to do something about it, right?

   To sum up we should remember about this feature of Flash Player when dealing with invalid dates and the correct operation for subtracting 1 month from existing date can be the following:

 
var startDate : Date = new Date (2011, 2, 31);
var initNewDate : Date = new Date(startDate.time)
var newDate : Date;
 
initNewDate.month--;				
newDate = handleShorterMonths(startDate, initNewDate, 1);
trace ("newDate=", newDate.toDateString()); //displays Mon Feb 28 2011
 
private function handleShorterMonths(startDate : Date, dateToCheck :Date, monthIndex : int) : Date {
   var correctDate : Date = dateToCheck;
   if (dateToCheck.date < startDate.date) {
        correctDate.date = startDate.date - dateToCheck.date;	
	correctDate.month = monthIndex;
   }
   return correctDate; 
}

Interactive sample with view source enabled that illustrates the issue:

Get Adobe Flash player

Alternatively, if you do not like to do Date Math operations by yourself, you can always rely on As3Commons DateUtils.as class.

This utility class has a very well written API to do various Math operations with Dates - you add days, months, years, etc. to existing date with an ease.

Google offers Flash 8 based version of Google Talk

In May 2006 Google had acquired small Gtalkr company that had it own IM client that was able to connect to Google Talk network.
And results of this Google acquisition from 2006 are clearly visible today in 2007.

And now, Ladies and gentlemen, please welcome, an official online version of Google Talk client from Google, released in 2007, done in Macromedia Flash 8.

To experience flash version of online Gtalk client, please visit the page http://www.google.com/talk/ and press button "Launch Google Talk".

google talk

Flash 9 == Flash CS3 and Adobe CS3 package is going to be ANNOUNCED later at this month

Accordingly to Macword.co.uk Adobe CS3 will be announced at 27th of March, 2007 at a special event in New York.

Although I do not have a precise information what is inside Adobe CS3 box, but the forthcoming renaming of Flash 9 to Flash CS3 gives me some hints that Flash 9 CAN be a part of Adobe CS3 package.

What do you think?

UPDATE: This is only an announcement date, the software is going to be actually shipped later in the spring'07.

Handling button events in Flash 8 by event capturing has some caveats

The situation is as follows:

I have an extenal asset in SWF format with some "onPress" button event handlers specified.

I load this external asset via MovieClipLoader class into the SWF container and all those "onPress" button event handlers became unresponsive because of the SWF container itself has specified event handlers for another! button events - "onRollOver" and "onRollOut".

Code for embedding external SWF asset follows:

var myMCL = new MovieClipLoader();//create an instance of MovieClipLoader
myMCL.loadClip("externalAsset.swf", "_root.image1_mc");
myMCL.onLoadComplete = function (targetMC) {
          image_mc.onRollOver = function() {
              trace ("onRollOver button handler works great,
              but my onPress button event handlers are lost");
 }
}

The article written by Trevor McCauley gave me an good insight to create an solution for this type of the problem, but I do not like the fact by itself that such a problem occurred =)

My solution is to substitute onRollOver event handler with onMouseMove event handler ( which is not a button event):

this.onMouseMove = onCustomMouseMove;
function onCustomMouseMove() {
 if (bgd_mc.hitTest(_root._xmouse, _root._ymouse, true))  {
  if (image1_mc._xscale < MAX_SCALE && bgd_mc._width == 215 &&
!myTween1.isPlaying) {
   growImage();
  }
 } else {
  if (image1_mc._xscale > MIN_SCALE && bgd_mc._width == MAX_WIDTH && !myTween1.isPlaying) {
   shrinkImage();
  }
 }
}

Mp3 player was released into production this week at Djuicefuns

Recently, I had programmed a pretty solid mp3 player in AS2, Flash 8. The resulted size of the SWF is 24kb, mainly because of the embedded PNG background bitmap. I relied on External Interface to communicate with JavaScript routines and it works flawlessly for me. SWFObject was used to embed the SWF into the HTML page
It feels really nice to release the product that will be used by 1000+ of people.

http://www.djuicefuns.com

The web-site language is Russian, it is social network with a focus on mobile users.

The mp3 player in particular allows to set 2 markers for the song to allow the cut of the song selection to become a ringtone at the mobile.

djuicefuns mp3 player

Current Flash Player 8 penetration rate has reached 50%!

Flash 8 player released : 9/13/05

As of December 2005 Flash 8 player penetration rate had reached 45.2% in USA.

Compare it with the speed of spread of previous major Flash player version - Flash player 7.

Flash 7 player released.: 8/25/03

As of December 2003 Flash 7 player penetration rate had reached 28.9% in USA.

Bottom line:
( taken from blog of Emmy Huang, Product Manager of the Flash Player )
This is 56% faster than Flash Player 6 and Flash Player 7 in the first quarters of those releases.
And, based on the server stats from our hosting company, Akamai, Flash Player 8 has been downloaded over 1.4 billion times since the September launch

Display special characters in Flash – use URL Encoding

    Recently I had to load the text from external source in Flash and from the design of the application I had to rely on LoadVars command.

    No wonder, that quite soon I came up with a problem that Flash does not display special characters like "+", "%" or "&" if those characters are loaded into Flash via LoadVars. After a bit of research, I found out that LoadVars object method "load" operates with URL encoded strings to allow to include those special characters into string of text.

    A good reference table with URL Encoding table for special characters can be found here at
Flash TechNote URL Encoding: Reading special characters from a text file.

The more extensive list of Unicode escape sequence codes in PDF format can be obtained from Unicode.org.

Therefore in Flash,

  • Symbol "%" becomes %25 after URL encoded
  • Symbol "&" becomes %26 after URL encoded
  • Symbol "+" becomes %2B after URL encoded
  • and so on...

    In ActionScript code:

    this.createTextField("result_ta",1, 0,0,150,20);
    result_ta.embedFonts =false;

    var myData_lv = new LoadVars();
    myData_lv.load ("myText.txt");
    myData_lv.onLoad = function()
    {
    result_ta.text = myData_lv.sp1_title_1;
    }

    Content of "myText.txt":

    &sp1_title_1=Andrew %26 Stas have gained a 5%25 percentage raise %2b good vacation bonus

        As a concluding note, an extensive paragraph on how to work with special characters in Flash 6 and higher is written at PDF file:"Using ActionScript in Flash" (Page 118 "Working with text")