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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 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
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
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)







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