---
title: "【教程】使用BitmapData来管理和进行纸牌游戏"
author: "txf2004"
date: "2007-12-28T20:31:00Z"
tags: []
slug: "bitmapdata"
draft: false
lastmod: "2007-12-28T20:31:00Z"
id: 606625
url: "https://txf2004.iteye.com/blog/606625"
---

导读：  
　　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.

我在前面的教程中曾讲过如何使用bitmapdata来洗牌，这回我要向大家展示的是，如何使用这个类来管理一堆的卡片

管理一堆的卡片式很有用的事情，因为库里有了这个之后，你就可以制作任何的卡片游戏了

![](http://www.emanueleferonato.com/images/cardz.jpg)

　　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.

首先你得有个象下面这样的卡片，当然因为是博客的原因这幅图片被缩放了，当然你可以追随链接下载其原始的尺寸。

现在就像在前面的一篇教程里见到的那样，你可以通过使用Bitmapdata类来，从这个图片中切出所有的卡片。我将发布关于这个问题的一些原始的as脚本，当然将会在后记的章节中给出全面的教程  
　　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.

这个简单的原型，大概是所有的纸牌中最简单的了：你抽出一张牌，然后猜测接下来发的这张牌是比桌上这张大呢？还是小呢？虽然并没有完成，但是我愿意和你分享这段代码  
　　**ACTIONSCRIPT:**  
　　importflash.display.BitmapData  
　　importflash.geom.Rectangle  
　　importflash.geom.Point  
　　importflash.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=0x　　**var**from = Math.floor(Math.random()\*52)  
　　**var**to = this\[x]  
　　this\[x]= this\[from]  
　　this\[from]= to;  
　　}  
　　}  
　　for(x=0x　　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&gt;210){  
　　this.\_x= 210  
　　this.action= "stay"  
　　}  
　　break  
　　case"move":  
　　this.\_x+= 20  
　　if(this.\_x&gt;310){  
　　this.\_x= 310  
　　this.action= "dissolve"  
　　}  
　　break  
　　case"dissolve":  
　　this.\_alpha-= 4  
　　if(this.\_alpha　　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.

在接下来的博客中我们还会继续讨论这个问题，不要错过这段代码  
　　&gt;&gt;Flash Templatesprovided 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.

本文转自  
[http://www.emanueleferonato.com/2007/12/26/using-bitmapdata-to-manage-a-deck-of-cards/](http://www.emanueleferonato.com/2007/12/26/using-bitmapdata-to-manage-a-deck-of-cards/)