您的位置:首页 > 其它

【教程】使用BitmapData来管理和进行纸牌游戏

2007-12-28 20:31 375 查看
导读:
  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来洗牌,这回我要向大家展示的是,如何使用这个类来管理一堆的卡片

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



  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
  vardistance:Number= 4
  varangleInDegrees:Number= 45
  varcolor:Number= 0x000000;
  varalpha:Number= .5
  varblurX:Number= 4
  varblurY:Number= 4
  varstrength:Number= 1
  varquality:Number= 3
  varinner:Boolean= false
  varknockout:Boolean= false
  varhideObject:Boolean= false
  varmy_shadow_filter:DropShadowFilter = newDropShadowFilter(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 = newArray()
  sequence = newArray()
  Array.prototype.shuffle= function(){
  for(x=0x<52x++){
  varfrom = Math.floor(Math.random()*52)
  varto = this[x]
  this[x]= this[from]
  this[from]= to;
  }
  }
  for(x=0x<52x++){
  card = _root.createEmptyMovieClip("small_pic_obj_"+x, _root.getNextHighestDepth())
  sequence[x]= x;
  small_picture = newBitmapData(79, 123)
  card.attachBitmap(small_picture,_root.getNextHighestDepth())
  small_picture.copyPixels(big_picture,newRectangle(0+(x%13)*79, 0+Math.floor(x/13)*123, 79, 123),newPoint(0, 0))
  card._visible= false
  card.filters= newArray(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()
  }
  }
  functiondraw_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.

在接下来的博客中我们还会继续讨论这个问题,不要错过这段代码
  >>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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: