protected function application1_creationCompleteHandler(event:FlexEvent):void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, loaderComplete );
loader.load( new URLRequest( "cards.png" ) );
}
/**
* Extract each card in the deck into their own object
* The object includes a label (rank of suit) and a Bitmap image of the card
*/
protected function loaderComplete( event:Event ):void
{
var suitsIdx:Number = 0;
var ranksIdx:Number = 0;
var cardWidth:Number = 79;
var cardHeight:Number = 123;
var a:Array = [];
var img:BitmapData;
var d:BitmapData = Bitmap( loader.content ).bitmapData;
for ( var i:int = 0; i < 52; i++ )
{
img = new BitmapData( cardWidth, cardHeight );
img.copyPixels( d, new Rectangle( ranksIdx * cardWidth, suitsIdx *
cardHeight, cardWidth, cardHeight ), new Point( 0, 0 ) );
img.draw( img, new Matrix() );
a.push( new Bitmap( img ) );
if ( i == 12 || i == 25 || i == 38 )
{
suitsIdx++;
ranksIdx = 0;
}
else
{
ranksIdx++;
}
}
list.dataProvider = new ArrayCollection(a);
list.selectedIndex = 25;
}
Wednesday, November 11, 2009
Creating in Memory BitMaps from a single image file.
I found this today while looking up information on the new Flex 4 Layout classes. This guy has a single .png file with a bunch of cards on it that he wants to use as individual images in his application. I was pretty impressed with the code, mainly because I had never really thought about it and it's an easy solution.
Subscribe to:
Post Comments (Atom)
1 comment:
Sorry about the poor word wrapping. You can just select the code and paste it somewhere else to see it more clearly
Post a Comment