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

导读：

还记得前几天说过的用Bitmap来管理纸牌游戏的帖子嘛，作者又发出了他最近的代码，介绍如下

　　At the moment the prototype has all messages and a game over screen with the score recap.  
　　Now I want to include Kongregate's highscores APIand then I'll releae the complete tutorial.

到现在为止，纸牌游戏的内容便告一段落了。下面我们将发布完整的教程，下面是代码  
　　Meanwhile, here it is the code:  
　　**ACTIONSCRIPT:**  
　　importflash.display.BitmapData  
　　importflash.geom.Rectangle  
　　importflash.geom.Point  
　　importflash.filters.DropShadowFilter  
　　importflash.filters.GlowFilter  
　　**var**card\_shadow:DropShadowFilter = **new**DropShadowFilter(4, 45, 0x000000, .5, 4, 4, 1, 3, **false**, **false**, **false**)  
　　**var**text\_glow:GlowFilter = **new**GlowFilter(0x000000, .6, 4, 4, 2, 3, **false**, **false**)  
　　big\_picture = BitmapData.loadBitmap("cardz")  
　　\_root.attachMovie("table", "table", \_root.getNextHighestDepth())  
　　\_root.createEmptyMovieClip("game", \_root.getNextHighestDepth())  
　　game.attachMovie("higher", "higher", game.getNextHighestDepth())  
　　game.attachMovie("lower", "lower", game.getNextHighestDepth())  
　　game.createEmptyMovieClip("big\_pic\_obj", game.getNextHighestDepth())  
　　big\_pic\_obj.attachBitmap(big\_picture, game.getNextHighestDepth())  
　　big\_pic\_obj.\_visible= **false**  
　　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 = game.createEmptyMovieClip("small\_pic\_obj\_"+x, game.getNextHighestDepth())  
　　sequence\[x]= x;  
　　small\_picture = **new**BitmapData(79, 123)  
　　card.attachBitmap(small\_picture, game.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(card\_shadow)  
　　card.onEnterFrame= **function**(){  
　　switch(this.action){  
　　case"come":  
　　this.\_x+= 40  
　　if(this.\_x&gt;210){  
　　this.\_x= 210  
　　this.action= "stay"  
　　if(cards\_drawn&gt;1){  
　　game.ok\_ko.\_visible= **true**  
　　}  
　　}  
　　break  
　　case"move":  
　　this.\_x+= 20  
　　if(this.\_x&gt;310){  
　　this.\_x= 310  
　　this.action= "dissolve"  
　　}  
　　break  
　　case"dissolve":  
　　this.\_alpha-= 4  
　　game.lap\_score.\_alpha-= 4  
　　if(this.\_alpha　　can\_draw = **true**  
　　game.ok\_ko.\_visible= **false**  
　　if(cards\_drawn == 52){  
　　endgame()  
　　}  
　　this.removeMovieClip()  
　　}  
　　break  
　　}  
　　}  
　　}  
　　game.attachMovie("ok\_ko", "ok\_ko", game.getNextHighestDepth(), {\_x:250, \_y:230, \_visible:**false**})  
　　game.attachMovie("score", "score", game.getNextHighestDepth(), {\_x:0, \_y:140})  
　　game.attachMovie("lap\_score", "lap\_score", game.getNextHighestDepth(), {\_x:-50, \_y:0, \_alpha:0})  
　　game.score.filters= **new**Array(text\_glow)  
　　game.lap\_score.filters= **new**Array(text\_glow)  
　　sequence.shuffle()  
　　cards\_drawn = 0  
　　points = 0  
　　can\_draw = **true**  
　　draw\_card()  
　　game.higher.onRelease= **function**(){  
　　if(can\_draw){  
　　can\_draw = **false**  
　　higher = **true**  
　　draw\_card()  
　　}  
　　}  
　　game.lower.onRelease= **function**(){  
　　if(can\_draw){  
　　can\_draw = **false**  
　　higher = **false**  
　　draw\_card()  
　　}  
　　}  
　　**function**draw\_card(){  
　　game.ok\_ko.gotoAndStop(2)  
　　lap = 0  
　　if(((sequence\[cards\_drawn]%13)=(sequence\[cards\_drawn-1]%13)and(higher))){  
　　game.ok\_ko.gotoAndStop(1)  
　　game.lap\_score.lap\_text.textColor= 0x40ff40;  
　　if(cards\_drawn&gt;0){  
　　if(!higher){  
　　lap = 13-(sequence\[cards\_drawn-1]%13)  
　　}else{  
　　lap = sequence\[cards\_drawn-1]%13+1  
　　}  
　　}  
　　}else{  
　　lap = -5  
　　game.lap\_score.lap\_text.textColor= 0x900000;  
　　}  
　　game.lap\_score.lap\_text.text= lap;  
　　if(cards\_drawn&gt;0){  
　　game.lap\_score.\_alpha= 100  
　　}  
　　points += lap;  
　　game\["small\_pic\_obj\_"+sequence\[cards\_drawn]].\_x= 0  
　　game\["small\_pic\_obj\_"+sequence\[cards\_drawn]].\_y= 10  
　　game\["small\_pic\_obj\_"+sequence\[cards\_drawn]].\_visible= **true**  
　　game\["small\_pic\_obj\_"+sequence\[cards\_drawn]].action= "come"  
　　game\["small\_pic\_obj\_"+sequence\[cards\_drawn-1]].action= "move"  
　　cards\_drawn++;  
　　game.score.textscore.text= "Cards left: "+(52-cards\_drawn)+" - Score: "+points;  
　　}  
　　**function**endgame(){  
　　game.removeMovieClip()  
　　\_root.attachMovie("game\_over","game\_over",\_root.getNextHighestDepth())  
　　game\_over.gameovertext.filters= **new**Array(text\_glow)  
　　\_root.game\_over.gameovertext.text= "Your score:**\\n**"+points;  
　　}  
　　and this is the result  
　　When it's game over, at the moment you must reload the page to play again. How much did you score?  
　　Download the source code, and wait for the full tut...  
　　&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.

【译者注：这里本来是有一个FLASH的，但是对剪影如何转载FLASH 内容不慎名了，好的是顺着链接也很容易找到了，怎么样，虽然游戏不怎么样，但是这么华丽的UI是不是也为其增添了几分趣味呢？】  
　　**7 Comment(s)**  
　　**Marukomu**| Dec 28, 2007| Reply  
　　Good stuff! This is great for work. I think I’m going rebuild one for my wife.  
　　i scored 74 :P  
　　I’m the recordman:  
　　90!  
　　Awesome!!! 69 btw \*\_^  
　　haha 104 winner :D  
　　54! hurray!!  
　　**limpeh**| Dec 29, 2007| Reply  
　　This game is not bad, but after some time, it’s quite boring since all you need to do is just ‘guessing’. (btw, I got 91)

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