Using BitmapData to manage a deck of cards
December 28th update: 2nd part online
I have already published a tutorial about BitmapData in the post Shuffle an image with BitmapData, but this time I want to show you how to manage a deck of cards using BitmapData.
Managing a deck of cards is very useful because there are tons of card games you can publish once you have a deck of cards in your library.
The first thing you need is an image with all cards like this one:

The image is scaled down for blogging reasons, but you can view/download it at its original size here.
Now, with the same method seen in Shuffle an image with BitmapData, I'll cut off all cards starting from the original image.
I am publishing some raw actionscript because this is just a prototype, but a full tutorial will come shortly.
The prototype is about the simplest card game ever: you draw a card and have to say if the next card will be higher on lower than the card you have on the table.
Being a prototype it's not complete yet, but I want to share this code to you.
-
import flash.display.BitmapData;
-
import flash.geom.Rectangle;
-
import flash.geom.Point;
-
import flash.filters.DropShadowFilter;
-
var distance:Number = 4;
-
var angleInDegrees:Number = 45;
-
var color:Number = 0x000000;
-
var alpha:Number = .5;
-
var blurX:Number = 4;
-
var blurY:Number = 4;
-
var strength:Number = 1;
-
var quality:Number = 3;
-
var inner:Boolean = false;
-
var knockout:Boolean = false;
-
var hideObject:Boolean = false;
-
var my_shadow_filter:DropShadowFilter = new DropShadowFilter(distance, angleInDegrees, color, alpha, blurX, blurY, strength, quality, inner, knockout, hideObject);
-
big_picture = BitmapData.loadBitmap("cardz");
-
_root.attachMovie("table","table",_root.getNextHighestDepth());
-
_root.attachMovie("higher","higher",_root.getNextHighestDepth());
-
_root.attachMovie("lower","lower",_root.getNextHighestDepth());
-
_root.createEmptyMovieClip("big_pic_obj",_root.getNextHighestDepth());
-
big_pic_obj.attachBitmap(big_picture,_root.getNextHighestDepth());
-
big_pic_obj._visible = false;
-
pictures = new Array();
-
sequence = new Array();
-
Array.prototype.shuffle = function() {
-
for (x=0; x<52; x++) {
-
var from = Math.floor(Math.random()*52);
-
var to = this[x];
-
this[x] = this[from];
-
this[from] = to;
-
}
-
};
-
for (x=0; x<52; x++) {
-
card = _root.createEmptyMovieClip("small_pic_obj_"+x, _root.getNextHighestDepth());
-
sequence[x] = x;
-
small_picture = new BitmapData(79, 123);
-
card.attachBitmap(small_picture,_root.getNextHighestDepth());
-
small_picture.copyPixels(big_picture,new Rectangle(0+(x%13)*79, 0+Math.floor(x/13)*123, 79, 123),new Point(0, 0));
-
card._visible = false;
-
card.filters = new Array(my_shadow_filter);
-
card.onEnterFrame = function() {
-
switch (this.action) {
-
case "come" :
-
this._x += 40;
-
if (this._x>210) {
-
this._x = 210;
-
this.action = "stay";
-
}
-
break;
-
case "move" :
-
this._x += 20;
-
if (this._x>310) {
-
this._x = 310;
-
this.action = "dissolve";
-
}
-
break;
-
case "dissolve" :
-
this._alpha -= 4;
-
if (this._alpha<0) {
-
can_draw=true;
-
this.removeMovieClip();
-
}
-
break;
-
}
-
};
-
}
-
sequence.shuffle();
-
cards_drawn = 0;
-
can_draw = true;
-
draw_card();
-
higher.onRelease = function() {
-
if(can_draw){
-
can_draw=false;
-
draw_card();
-
}
-
};
-
lower.onRelease = function() {
-
if(can_draw){
-
can_draw=false;
-
draw_card();
-
}
-
};
-
function draw_card() {
-
_root["small_pic_obj_"+sequence[cards_drawn]]._x = 0;
-
_root["small_pic_obj_"+sequence[cards_drawn]]._y = 30;
-
_root["small_pic_obj_"+sequence[cards_drawn]]._visible = true;
-
_root["small_pic_obj_"+sequence[cards_drawn]].action = "come";
-
_root["small_pic_obj_"+sequence[cards_drawn-1]].action = "move";
-
cards_drawn++;
-
}
and the result is...
I think simple and stupid games like this one can have a good replay value if I provide a high score table like in Christmas Couples, another simple game that entered in the "million games played" club.
But I will talk about it in the next post about the experiment, meanwhile take the source code.
Read 2nd part
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
6 Responses to “Using BitmapData to manage a deck of cards”
Leave a Reply

this is amazing, can’t wait for the tutorial!
thanks for all the help you’ve given!
damn you got 1.000.000+ on christmas couples :o.
lucky you ^^
Good Idea :D!
nice.
^_^
Interesting,how’s Tileball going?
Lol I bet you got most of those plays on Christmas Couples from Mindjolt.com