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:

Deck of cards

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

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

6 Responses to “Using BitmapData to manage a deck of cards”

  1. David on December 26th, 2007 8:33 pm

    this is amazing, can’t wait for the tutorial!

    thanks for all the help you’ve given!

  2. styxtwo on December 26th, 2007 10:03 pm

    damn you got 1.000.000+ on christmas couples :o.
    lucky you ^^

  3. Thomas on December 27th, 2007 4:37 am

    Good Idea :D!

  4. shiv on December 27th, 2007 7:04 pm

    nice.
    ^_^

  5. RJ on December 28th, 2007 1:14 pm

    Interesting,how’s Tileball going?

  6. stupid on December 29th, 2007 7:26 pm

    Lol I bet you got most of those plays on Christmas Couples from Mindjolt.com

Leave a Reply




flash games company