html5游戏开发-零基础开发RPG游戏-开源讲座(三)

了解上两篇的内容请点击:

html5游戏开发-零基础开发RPG游戏-开源讲座(一)

http://www.html5cn.org/article-1737-1.html

html5游戏开发-零基础开发RPG游戏-开源讲座(二)-跑起来吧英雄

http://www.html5cn.org/article-1738-1.html

前两篇,RPG的开发已经实现了添加地图和添加游戏人物,本篇来实现地图的卷轴滚动和人物对话的实现,效果如下

1111.gif


 



本次开发,更新了一下库件至1.3,请点击下面的链接,下载库件1.3版以上版本

http://code.google.com/p/legendforhtml5programming/downloads/list

地图的滚动

关于地图的滚动原理,可以参照下图

1212.gif


 



按照上图说明,实现地图滚动,只需要先把即将出现的地图(图中黄色部分)画上,然后滚动地图,待地图滚动完毕之后,将屏幕之外的部分(图中绿色部分)移除

首先要添加一个变量来控制地图是否滚动

  1. //地图滚动
  2. var mapmove = false;

然后,在人物移动的时候,判断地图是否需要滚动


  1. Character.prototype.checkMap = function (dir){
  2.         var self = this;
  3.         mapmove = false;
  4.         //如果不是英雄,则地图不需要滚动
  5.         if(!self.isHero)return;
  6.         
  7.         switch (dir){
  8.                 case UP:
  9.                         if(self.y + charaLayer.y> STEP)break;
  10.                         if(mapLayer.y >= 0)break;
  11.                         addMap(0,-1);
  12.                         mapmove = true;
  13.                         break;
  14.                 case LEFT:
  15.                         if(self.x + charaLayer.x > STEP)break;
  16.                         if(mapLayer.x >= 0)break;
  17.                         addMap(-1,0);
  18.                         mapmove = true;
  19.                         break;
  20.                 case RIGHT:
  21.                         if(self.x < 480 - 2*STEP)break;
  22.                         if(480 - mapLayer.x >= map[0].length*STEP)break;
  23.                         addMap(1,0);
  24.                         mapmove = true;
  25.                         break;
  26.                 case DOWN:
  27.                         if(self.y < 288 - 2*STEP)break;
  28.                         if(288 - mapLayer.y >= map.length*STEP)break;
  29.                         addMap(0,1);
  30.                         mapmove = true;
  31.                         break;
  32.         }
  33. };

在移动过程中,判断地图是否处于滚动状态,如果地图处于滚动,则滚动地图,否则移动人物


  1. Character.prototype.onmove = function (){
  2.         var self = this;
  3.         //设定一个移动步长中的移动次数
  4.         var ml_cnt = 4;
  5.         //计算一次移动的长度
  6.         var ml = STEP/ml_cnt;
  7.         //根据移动方向,开始移动
  8.         switch (self.direction){
  9.                 case UP:
  10.                         if(mapmove){
  11.                                 mapLayer.y += ml;
  12.                                 charaLayer.y += ml;
  13.                         }
  14.                         self.y -= ml;
  15.                         break;
  16.                 case LEFT:
  17.                         if(mapmove){
  18.                                 mapLayer.x += ml;
  19.                                 charaLayer.x += ml;
  20.                         }
  21.                         self.x -= ml;
  22.                         break;
  23.                 case RIGHT:
  24.                         if(mapmove){
  25.                                 mapLayer.x -= ml;
  26.                                 charaLayer.x -= ml;
  27.                         }
  28.                         self.x += ml;
  29.                         break;
  30.                 case DOWN:
  31.                         if(mapmove){
  32.                                 mapLayer.y -= ml;
  33.                                 charaLayer.y -= ml;
  34.                         }
  35.                         self.y += ml;
  36.                         break;
  37.         }
  38.         self.moveIndex++;
  39.         //当移动次数等于设定的次数,开始判断是否继续移动
  40.         if(self.moveIndex >= ml_cnt){
  41.                 //一个地图步长移动完成后,如果地图处于滚动状态,则移除多余地图块
  42.                 if(mapmove)delMap();
  43.                 self.moveIndex = 0;
  44.                 //如果已经松开移动键,或者前方为障碍物,则停止移动,否则继续移动
  45.                 if(!isKeyDown || !self.checkRoad()){
  46.                         self.move = false;
  47.                         return;
  48.                 }else if(self.direction != self.direction_next){
  49.                         self.direction = self.direction_next;
  50.                         self.anime.setAction(self.direction);
  51.                 }
  52.                 //地图是否滚动
  53.                 self.checkMap(self.direction);
  54.         }
  55. };

最后,将地图的数组和地形扩大为大于屏幕大小

  1. //地图图片数组
  2. var map = [
  3. [18,18,18,18,18,18,18,18,18,18,18,18,55,55,18,18,18],
  4. [18,18,18,17,17,17,17,17,17,17,17,17,55,55,17,17,18],
  5. [18,18,17,17,17,17,18,18,17,17,17,17,55,55,17,17,18],
  6. [18,17,17,17,18,18,18,18,18,17,17,55,55,17,17,17,18],
  7. [18,17,17,18,22,23,23,23,24,18,17,55,55,17,17,17,18],
  8. [18,17,17,18,25,28,26,79,27,18,55,55,17,17,17,17,18],
  9. [18,17,17,17,17,10,11,12,18,18,55,55,17,17,17,17,18],
  10. [18,18,17,17,10,16,16,16,11,55,55,17,17,17,17,17,18],
  11. [18,18,17,17,77,16,16,16,16,21,21,17,17,17,17,17,18],
  12. [18,18,17,17,77,16,16,16,16,55,55,17,17,17,17,17,18],
  13. [18,18,18,18,18,18,18,18,18,55,55,18,18,18,18,18,18]
  14. ];
  15. //地图地形数组
  16. var mapdata = [
  17. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  18. [1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,1],
  19. [1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,1],
  20. [1,0,0,0,1,1,1,1,1,0,0,1,1,0,0,0,1],
  21. [1,0,0,1,1,1,1,1,1,1,0,1,1,0,0,0,1],
  22. [1,0,0,1,1,1,0,1,1,1,1,1,0,0,0,0,1],
  23. [1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1],
  24. [1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1],
  25. [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  26. [1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1],
  27. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
  28. ];

为了实现地图滚动,修改添加地图的方法,根据参数来实现添加上面图片的黄色地图部分

  1. //添加地图
  2. function addMap(cx,cy){
  3.         var i,j,index,indexX,indexY;
  4.         var bitmapdata,bitmap;
  5.         var mapX = mapLayer.x / STEP;
  6.         var mapY = mapLayer.y / STEP;
  7.         var mx = cx<0?-1:0,my = cy<0?-1:0;
  8.         if(imageArray == null){
  9.                 //地图图片数据
  10.                 bitmapdata = new LBitmapData(imglist["map"]);
  11.                 //将地图图片拆分,得到拆分后的各个小图片的坐标数组
  12.                 imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,10,10);
  13.         }
  14.         mapLayer.removeAllChild();
  15.         //在地图层上,画出15*10的小图片
  16.         for(i=my;i<9 +Math.abs(cy) && i-mapY < map.length;i++){
  17.                 for(j=mx;j<15 +Math.abs(cx)&& j-mapX < map[0].length;j++){
  18.                         //从地图数组中得到相应位置的图片坐标
  19.                         index = map[i-mapY][j-mapX];
  20.                         //小图片的竖坐标
  21.                         indexY = Math.floor(index /10);
  22.                         //小图片的横坐标
  23.                         indexX = index - indexY*10;
  24.                         //得到小图片
  25.                         bitmapdata = new LBitmapData(imglist["map"],indexX*32,indexY*32,32,32);
  26.                         bitmap = new LBitmap(bitmapdata);
  27.                         //设置小图片的显示位置
  28.                         bitmap.x = j*STEP - mapLayer.x;
  29.                         bitmap.y = i*STEP - mapLayer.y;
  30.                         //将小图片显示到地图层
  31.                         mapLayer.addChild(bitmap);
  32.                 }
  33.         }
  34. }
  35. //移除多余地图块
  36. function delMap(){
  37.         var bitmap,i;
  38.         for(i=0;i<mapLayer.childList.length;i++){
  39.                 bitmap = mapLayer.childList[i];
  40.                 if(bitmap.x + mapLayer.x < 0 || bitmap.x + mapLayer.x >= 480 || 
  41.                                 bitmap.y + mapLayer.y < 0 || bitmap.y + mapLayer.y >= 288){
  42.                         mapLayer.removeChild(bitmap);
  43.                         i--;
  44.                 }
  45.         }
  46. }

看一下效果如下

1441.gif


 



人物的对话

对话的实现,在点击控制按钮的方形按钮时添加,所以,先在鼠标抬起的时候,判断是否点击了方形按钮

  1. function onup(event){
  2.         isKeyDown = false;
  3.         if(event.offsetX >= ctrlLayer.x + 280 && event.offsetX <= ctrlLayer.x+330){
  4.                 if(event.offsetY >= ctrlLayer.y+40 && event.offsetY <= ctrlLayer.y+100){
  5.                         //对话
  6.                         addTalk();
  7.                 }
  8.         }
  9. }

在完善addTalk()方法的时候,首先准备好对话的内容

  1. var talkScriptList = {
  2.         "talk1":new Array(
  3.                 {img:"m",name:"鸣人",msg:"我是木叶村的鸣人,你是谁?"},
  4.                 {img:"n",name:"黑衣忍者甲",msg:"你就是鸣人?九尾还在你身体里吗?"}
  5.                 ),
  6.         "talk2":new Array(
  7.                 {img:"n",name:"黑衣忍者乙",msg:"鸣人,听说忍者大战就要开始了。"},
  8.                 {img:"m",name:"鸣人",msg:"真的吗?一定要想想办法啊。"}
  9.                 )
  10. };

talk1,talk2中talk后面的数字,代表人物的编号,其中每个对话单位的img为人物的头像,name为人物的名称,msg为对话的内容

添加对话时的做法是,当点击方形按钮后,判断小鸣人前方是否有人,如果有人,则将这个人物的编号取出来,再从上面的数组中获取相应的对话内容,然后,将相应的内容显示到游戏屏幕上,具体实现代码如下

  1. //对话内容
  2. var talkScript;
  3. var talkScriptList = {
  4.         "talk1":new Array(
  5.                 {img:"m",name:"鸣人",msg:"我是木叶村的鸣人,你是谁?"},
  6.                 {img:"n",name:"黑衣忍者甲",msg:"你就是鸣人?九尾还在你身体里吗?"}
  7.                 ),
  8.         "talk2":new Array(
  9.                 {img:"n",name:"黑衣忍者乙",msg:"鸣人,听说忍者大战就要开始了。"},
  10.                 {img:"m",name:"鸣人",msg:"真的吗?一定要想想办法啊。"}
  11.                 )
  12. };
  13. //对话序号
  14. var talkIndex = 0;
  15. //对话中
  16. var talking = false;
  17. function addTalk(){
  18.         //如果对话内容为空,则开始判断是否可以对话
  19.         if(talkScript == null){
  20.                 var key,tx = player.x,ty = player.y;
  21.                 switch (player.direction){
  22.                 case UP:
  23.                         ty -= STEP;
  24.                         break;
  25.                 case LEFT:
  26.                         tx -= STEP;
  27.                         break;
  28.                 case RIGHT:
  29.                         tx += STEP;
  30.                         break;
  31.                 case DOWN:
  32.                         ty += STEP;
  33.                         break;
  34.                 }
  35.                 for(key in charaLayer.childList){
  36.                         //判断前面又没有npc,有则开始对话
  37.                         if(charaLayer.childList[key].x == tx && charaLayer.childList[key].y == ty){
  38.                                 if(talkScriptList["talk"+charaLayer.childList[key].index]){
  39.                                         talkScript = talkScriptList["talk"+charaLayer.childList[key].index];
  40.                                         talkIndex = 0;
  41.                                 }
  42.                         }
  43.                 }
  44.                 //如果前方没有npc,则返回
  45.                 if(talkScript == null)return;
  46.         }
  47.         //将对话层清空
  48.         talkLayer.removeAllChild();
  49.         //当对话开始,且按照顺序进行对话
  50.         if(talkIndex < talkScript.length){
  51.                 //得到对话内容
  52.                 var talkObject = talkScript[talkIndex];
  53.                 //对话背景
  54.                 bitmapdata = new LBitmapData(imglist["talk"]);
  55.                 bitmap = new LBitmap(bitmapdata);
  56.                 bitmap.width = 330;
  57.                 bitmap.height = 70;
  58.                 bitmap.x = 100;
  59.                 bitmap.y = 20;
  60.                 bitmap.alpha = 0.7;
  61.                 talkLayer.addChild(bitmap);
  62.                 //对话头像
  63.                 bitmapdata = new LBitmapData(imglist[talkObject.img]);
  64.                 bitmap = new LBitmap(bitmapdata);
  65.                 bitmap.x = 0;
  66.                 bitmap.y = 0;
  67.                 talkLayer.addChild(bitmap);
  68.                 //对话人物名称
  69.                 var name = new LTextField();
  70.                 name.x = 110;
  71.                 name.y = 30;
  72.                 name.size = "14";
  73.                 name.color = "#FFFFFF";
  74.                 name.text = "[" + talkObject.name + "]";
  75.                 talkLayer.addChild(name);
  76.                 //对话内容
  77.                 var msg = new LTextField();
  78.                 msg.x = 110;
  79.                 msg.y = 55;
  80.                 msg.color = "#FFFFFF";
  81.                 msg.text = talkObject.msg;
  82.                 talkLayer.addChild(msg);
  83.                 //对话内容逐字显示
  84.                 msg.wind();
  85.                 talkLayer.x = 20;
  86.                 talkLayer.y = 50;
  87.                 talkIndex++;
  88.         }else{
  89.                 //对话结束
  90.                 talkScript = null;
  91.         }
  92. }

效果看下图

1515.gif


 




游戏演示地址

http://fsanguo.comoj.com/html5/rpg3/index.html

之前其他地方也稍微做了修改,具体修改请看源代码,此次更新源代码,下载地址如下

http://legend-demo.googlecode.com/files/rpg3.zip


 
posted @ 2012-02-03 14:29  HTML5中国  阅读(191)  评论(0编辑  收藏  举报