More control over the dataGrid component

When using the dataGrid component being lazy as me, you would just get the column names from your dataProvider

//name and rating are the field names in the dataProvider
myGrid.columns=["name","rating"]

and call it a day...
But that leaves you with evenly spaced columns and without any dynamic height of your grid, so you better resort to something like:

var nameCol:DataGridColumn=new DataGridColumn("name");
nameCol.headerText="Game";
nameCol.width=150;

var ratingCol:DataGridColumn=new DataGridColumn("rating");
ratingCol.headerText="Rating";
ratingCol.width=60;

myGrid.columns = [nameCol, ratingCol];
myGrid.width=210;

myGrid.dataProvider=new DataProvider(dp);
//auto-height
myGrid.rowCount=myGrid.length;

which will let you size the columns at your will and have the component grow as you plug in more entries.

Dude, where's my DragOver?

Having it not used for a long time I found out today that AS3 has no DragOver in its MouseEvents. So after a bit of research dugged out several solutions with a custom Event Class but finally resorted to this little if.. statement in my Event Handler for MOUSE_OVER:

//pcont serves as a container for about 100 clips 
//inside that will react to dragOver 
 
//activate on Mousedown immediately
pcont_mc.addEventListener(MouseEvent.MOUSE_DOWN,onDragOver);
pcont_mc.addEventListener(MouseEvent.MOUSE_OVER,onDragOver);

function onDragOver(e:MouseEvent):void
{
  if(e.buttonDown)
   {
     //the Mouse is pressed, 
     //Do something here
   }
}


the buttonDown Property of the MouseEvent makes sure that the Mouse is pressed while you hover your mc.
A more advanced solution can be found at Andre Michelle, he wrote some class files and stuff to adress this shortcoming of Events.

attachMovie in AS3

Today I wrote a little replacement snippet for the deprecated attachMovie. The snippet has 3 parameters for the linkage name in the library, the name of the displayObject you wish to attach the MC to and an optional name param to identify it by name on the display list later on:


function attachMovieClip(mcName:String,target:MovieClip,newName:String="")
{
var mc=new (getDefinitionByName(mcName) as Class);
mc.name = newName;
target.addChild(mc);
}

along with Lee Brimelows very useful snippet panel for Flash CS4 attaching MovieClips from the Library at runtime is a bit less complicated especially for AS2 veterans ;-).

the xml snippet for the snippet panel is here for convenient copy/paste:

<snippet>
<title>attachMovie AS3</title>
<code><![CDATA[function attachMovieClip(mcName:String,target:MovieClip,newName:String="")
{
    var mc=new (getDefinitionByName(mcName) as Class);
    mc.name=newName;
    target.addChild(mc);
}]]></code>
</snippet>

Have fun!