Using caurina Tweener

Some of you might be asking why use a third party tweening class when Flash has native code for it. The answers are:

  1. The native one has bugs. For example if you are simultaneously tweening 3 objects with native Tween class, from time to time you notice that one or more Tweens simply stop (at least this happened to me a lot while I was using the native class).
  2. You need 1 tween / tweened property. (If you move an object both on x and y you need two Tweens)
  3. It uses the same event model as everything in AS3(This is not necessarily bad, bit with Caurina Tweener you can not only specify the name of the handling function, you can also specify its parameters)
While Caurina Tweener:
  1. Works fine regardless of the Tweened objects.
  2. Gives better performance.
  3. You can tween multiple properties of 1 object with a single line of code
  4. It's built with static functions, meaning that you don't need an instance of the class every time you want to make a tween.
Here is a simple example:

The black box having instance name "box", and the initial alpha being 0.5 the code is:
import caurina.transitions.Tweener;
Tweener.addTween(box, {x:300, y:100, alpha:1, time:2, transition:"linear"});

Animation sequences are also much easer to make, as you no longer need to synchronize your tweens using events, for example:

There are practically two tweens one that changes x,y and alpha, it lasts 2 seconds as you can see in the code above, and one that changes the rotation.
The code is:
import caurina.transitions.Tweener;
Tweener.addTween(box,{x:300, y:100, alpha:1, time:2, transition:"linear"});
Tweener.addTween(box,{rotation:50, time:1, transition:"linear",delay:2.5});

No events no nothing just one extra parameter: "delay". As you can see it's set to 2.5, the length of the first tween is 2, so practicly the second tween starts 0.5 seconds after the first one finishes.
Still you may want to do something when your tween finishes playing, you accomplished this by 'addEventListener(Event.COMPLETE)' with tweener it's just another parameter as such:
Tweener.addTween(box, {rotation:50, time:1, transition:"linear",delay:2.5, onComplete:doThis});
function doThis()
{
    trace("here");
} 

doThis will be called when tween has completed. As you can see doThis doesn't have any parameters, wich might be good, but in case you do need parameters, that is easy to acomplish as well:
Tweener.addTween(box,{rotation:50, time:1, transition:"linear",delay:2.5, onComplete:doThis, onCompleteParams:["hello world"]});
function doThis(s:String)
{
   trace(s);
} 

NOTE: "hello world" can be replaced with a variable, or with multiple variables separated with comas. For example:
var nr1:Number=1;
var nr2:Number=2;
var nr3:Number=3;
Tweener.addTween(box,{rotation:50, time:1, transition:"linear",delay:2.5, onComplete:doThis,
onCompleteParams:[nr1,nr2,nr3]});
function doThis(n1:Number, n2:Number, n3:Number)
{
     trace(n1+n2+n3);
}


You can download examples from here.
You can read the online documentation for Tweener here.
You can download Tweener from here.

Hope somebody will find this usefull,
Best regards.

Changeing text formating for individual cells of a DataGrid in Flash AS3

So lets start with the obviousness, create a new AS3 fla, and drag a DataGrid component to the stage. I will name it dataG.
Put this code on the frame:

import fl.data.DataProvider;
var xml:XML=<tickets>
                <item>
                    <ID>231</ID>
                    <Status>Modified</Status>
                </item>
                <item>
                    <ID>232</ID>
                    <Status>Confirmed</Status>
                </item>
                <item>
                    <ID>233</ID>
                    <Status>Pending</Status>
                </item>
                </tickets>;
dataG.dataProvider= new DataProvider(xml)

If you publish the movie now, it should look something like this:

As the DataGrid dose not give us any direct access to individual cells (unless you asign them events), I feel the easyest way to achive the goal is to use the CellRenderer.
So we are going to make our own CellRenderer. To do this, open a new AS file, and write this code:
package
{
    import fl.controls.listClasses.CellRenderer;
    import flash.text.TextFormat;
    import fl.controls.listClasses.ListData  
    public class customRender extends CellRenderer
    {
        public function customRender():void
        {
          
        }
        public override function set listData(value:ListData):void//Override the seter function of listData
        {
            var tf:TextFormat = new TextFormat();
            tf.bold = true;
            super.listData = value;//call to the original seter function
            switch (data.Status)//Set the right color for this cell
            {
                case "Pending":
                    tf.color = 0xFF0000;
                break;
                case "Modified":
                    tf.color=0xFF6600;
                break;
                case "Confirmed":
                    tf.color=0x009900;
                break;
            }
            setStyle("textFormat",tf)//Apply text format          
          
        }
    }
  
}
To explain a bit, you are overriding the setter of the listData property ("public override function set listData(value:ListData):void"). You need to override this function as only when listData is specified the DataGridColumn object knows what he needs to display. After this, it is pretty strait forward. All you do is decide what color should be applied to the cell.
So now that you have the custom CellRenderer, all you have to do, is to assign it to the last column. You can do that by adding this to the code in your .fla: dataG.columns[1].cellRenderer=customRender;
Publishing it should result this:

You can download working code here.
Hope some of you will find it this usefull.

Keyboard event not working

To begin lets consider the following case: You are making a pac-man game, you want your user to have some control over the game so you place a menu button, you write the code for your menu. After you did this you notice that you no longer can control your pacman. After some headache you find out that if you click anything (except the menu button), your games works again. More technically speaking after doing removeChild to remove your menu your KeyboardEvent.KEY_DOWN/UP added to stage isn't firring any more. Example:

Just as described above you will see that the label at the top of the swf indicates correctly if a button is pressed or not UNTIL I remove the menu. Code is as follows:

var mainM:MovieClip;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyD);
stage.addEventListener(KeyboardEvent.KEY_UP,keyU);
btn.addEventListener(MouseEvent.CLICK,makeM);
function keyD(e:Event)
{
    textField.text="Key pressed";
}
function keyU(e:Event)
{
    textField.text="Key not pressed";
}
function makeM(e:Event)
{
    if (mainM==null)
    {
        mainM=new menu();
        addChild(mainM)
        mainM.x=0;
        mainM.y=60;
    }
}
function destroyM(e:Event)//function called from mainM that tells the SWF that the menu needs to close.
{
    removeChild(mainM)
    mainM=null;
}
"menu" is in my library and has Export for ActionScript.
So here's the part most people don't figure out (because its not really rational): though i did remove the menu from the stage, and eliminated any reference to it, since i clicked on the "Close Me" button it got focus, and it kept the focus even after being removed. (This is the irrational part... why would you want the focus on something that you cant interact with.). So let's recap, stage.addEventListener(KeyboardEvent.KEY_DOWN,example) is supposed to bubble to the stage, but the item that has focus, and there for is getting the event is no longer a child of the stage, this means the event will never bubble up back to the stage. The solution for this long and stupid problem is one tiny line:
stage.focus=anyMCOnStage;
In my case all i have to do is to add this line:
stage.focus=stage;
to my destroyM function. The result being:

Hope someone will find this helpful. Best regards.