LayaAir引擎——关于地图详解

所需要的软件:

LayaAirIDE1.0.2版本

在LayaAir引擎中与地图相关的类:

  1.laya.map.TiledMap      TiledMap类    地图以层级来划分地图,  每层又以分块来处理显示对象

  2.laya.maths.Rectangle      矩形类

  3.laya.events.Event          事件类

  4.laya.map.MapLayer      层级类

  5.laya.map.GridSprite       分块类

  6.laya.map.TileTexSet         纹理类

  7.laya.display.Sprite        精灵类

所需要的文件:

  

TiledMap(XML格式):

1.floor层    自定义属性  floor_pro    floor pro

2.wall层     自定义属性  wall_pro    wall pro

3.objLayer层  自定义属性  objLayer_pro   objLayer pro

         对象一    pro1    自定义属性  pro1_1  pro1_1 pro1_1

         对象二    pro2    自定义属性  pro2_2  pro2_2

1.为了方便调用类定义的全局变量

  1. var TiledMap = Laya.TiledMap;
  2. var Rectangle = Laya.Rectangle;
  3. var Handler = Laya.Handler;
  4. var Sprite = Laya.Sprite;
  5. var MapLayer = Laya.MapLayer;

2.在LayaAir项目中加载Tiled地图

  1. Laya.init(1200 , 720);
  2.  
  3. var map1 = new TiledMap();
  4. map1.createMap("map/map3/map3.json",new Rectangle(0,0,1200,720),Handler.create(this,loadMap_1_OK));
  5.  
  6. function loadMap_1_OK() {
  7. console.log("地图三加载完成");
  8. }

Laya.init(1200,720);      创建一个1200*720的窗口

var map1 = new TiledMap();   定义map1的类型是TiledMap类型

map1.createMap("map/map3/map3.json",new Rectangle(0,0,1200,720),Handler.create(this,loadMap_1_OK));

此函数是加载1200*720的在map/map3文件下的map3地图到map1中,加载完成自动调用函数loadMap_1_OK();

3.测试TiledMap属性

  1. function loadMap_1_OK() {
  2. console.log("地图三加载完成");
  3.  
  4. console.log(map1.x);//-0,地图x坐标
  5. console.log(map1.y);//-0,地图y坐标
  6.  
  7. console.log(map1.width);//240,48*5,地图的宽度
  8. console.log(map1.tileWidth);//48,格子的宽度
  9. console.log(map1.gridWidth);//432,块的宽度(不大懂,猜想48*5+48*5-32)
  10.  
  11. console.log(map1.height);//240,48*5,地图的高度
  12. console.log(map1.tileHeight);//48,格子的高度
  13. console.log(map1.gridHeight);//432,块的高度(不大懂,猜想48*5+48*5-32)
  14.  
  15. console.log(map1.numRowsTile);//5,地图竖向的格子数
  16. console.log(map1.numRowsGrid);//1,地图的竖向块数
  17.  
  18. console.log(map1.numColumnsTile);//5,地图横向的格子数
  19. console.log(map1.numColumnsGrid);//1,地图的横向块数
  20.  
  21. console.log(map1.orientation);//orthogonal,当前地图的类型
  22. console.log(map1.renderOrder);//right-down,渲染顺序是从右下开始。

  23. console.log(map1.scale);//1,当前地图的缩放
  24. }

gridWidth和gridHeight的源码计算分析:(感觉有点问题,但不深入)

  1. TiledMap初始化默认值:
  2. this._mapTileW=;//格子的宽度
  3. this._mapTileH=0;//格子的高度
  4. this._gridW=0;//地图的横向块数
  5. this._gridH=0;//地图的坚向块数
  6. this._gridWidth=450;//块的宽度
  7. this._gridHeight=450;//块的高度
  8.  
  9. createMap(mapName,viewRect,completeHandler,viewRectPadding,gridSize){
  10. ...
  11. if (gridSize){
  12. this._gridWidth=gridSize.x;//450
  13. this._gridHeight=gridSize.y;//450
  14. }
  15. ...
  16. }
  17.  
  18. onJsonComplete(e){
  19. ...
  20. this._mapTileW=tJsonData.tilewidth;//48
  21. this._mapTileH=tJsonData.tileheight;//48
  22. ...
  23. }
  24.  
  25. initMap(){
  26. ...
  27. this._gridWidth=Math.floor(this._gridWidth / this._mapTileW)*this._mapTileW;//432=9*48
  28. this._gridHeight=Math.floor(this._gridHeight / this._mapTileH)*this._mapTileH;//432=9*48
  29. if (this._gridWidth < this._mapTileW){
  30. this._gridWidth=this._mapTileW;
  31. }
  32. if (this._gridWidth < this._mapTileH){
  33. this._gridWidth=this._mapTileH;
  34. }
  35. this._gridW=Math.ceil(this._width / this._gridWidth);//1
  36. this._gridH=Math.ceil(this._height / this._gridHeight);//1
  37. ...
  38. }

orientation的补充:

  1. this._orientation="orthogonal";//当前地图类型为四边形地图
  2.  
  3. 地图类型:
  4.  
  5. hexagonal 六边形地图
  6.  
  7. isometric 菱形地图
  8.  
  9. orthogonal 四边形地图
  10.  
  11. staggered 45度交错地图

renderOrder的补充:

  1. this._renderOrder="right-down";//地图格子的渲染顺序为从右上角开始渲染
  2.  
  3. 地图格子的渲染顺序:
  4.  
  5. left-down    从左下角开始渲染
  6.  
  7. left-up      从左上角开始渲染
  8.  
  9. right-down    从右下角开始渲染
  10.  
  11. right-up     从有上角开始渲染

4.测试公开的方法

  1. var map1 = new TiledMap();//定义map1的类型是TiledMap类型

  2. map1.createMap("map/map3/map3.json",new Rectangle(0,0,240,240),Handler.create(this, mapLoaded));
    //把map/map3/map3.json以的显示240*240视图加载到map1中,加载完成调用mapLoaded()函数
  3. function mapLoaded(){
  4. console.log("地图加载");
  5. map1.destroy();//销毁map1地图
  6. console.log("地图销毁")
  7. }
  1. var a = map1.getLayerByIndex(0);//通过索引得MapLayer 层
  2. console.log(a.layerName);//floor
  3.  
  4. var a = map1.getLayerByIndex(1););//通过索引得MapLayer层
  5. console.log(a.layerName);//wall
  6.  
  7. var a = map1.getLayerByIndex(2););//通过索引得MapLayer层
  8. console.log(a.layerName);//obLayer
  9.  
  10. var a = map1.getLayerByName("floor");//通过名字得到MapLayer层
  11. console.log(a.layerName);//floor
  12.  
  13. var a = map1.getLayerByName("wall");//通过名字得到MapLayer层
  14. console.log(a.layerName);//wall
  15.  
  16. var a = map1.getLayerByName("objLayer");//通过名字得到MapLayer层
  17. console.log(a.layerName);//obLayer
  1. var b = map1.getLayerObject("objLayer","pro1");//得到对象层上的某一个物品
  2. console.log(b);//GridSprite {relativeX:24,relativeY:-24,......}
  3.  
  4. var b = map1.getLayerObject("objLayer","pro2");//得到对象层上的某一个物品
  5. console.log(b);//GridSprite {relativeX:216,relativeY:168,......}

注意:

realativeX和relativeY暂时不管

  1. var mX = 48;//X轴的偏移量
  2. var mY = 96;//y轴的偏移量
  3.  
  4. map1.moveViewPort(mX,mY);//地图向右边平移48像素,向下平移96像素
  1. var mX = 48;//X轴的偏移量
  2. var mY = 96;//y轴的偏移量
  3. var width = 240;
  4. var height = 240;
  5. map1.changeViewPort(mX,mY,width,height);//改变视口大小和位置
  1. var point = new Point();
  2. map1.setViewPortPivotByScale(0.5,0.5);//设置视口的缩放中心点(例如:scaleX= scaleY= 0.5,就是以视口中心缩放)
  3. map1.changeViewPortBySize(480,480,point);//在锚点的基础上计算,通过宽和高,重新计算视口
  1. changeViewPortBySize(width,height,rect){
  2. this._centerX=this._rect.x+this._rect.width *this._pivotScaleX;
  3. this._centerY=this._rect.y+this._rect.height *this._pivotScaleY;
  4. rect.x=this._centerX-width *this._pivotScaleX;
  5. rect.y=this._centerY-height *this._pivotScaleY;
  6. rect.width=width;
  7. rect.height=height;
  8. this.changeViewPort(rect.x,rect.y,rect.width,rect.height);
  9. }
  1. var b = map1.mapSprite();//整个地图的显示容器
  2. var c = new Sprite();
    c.loadImage("map/map3/map2_2.png",0,0,48,48,null);
    b.addChild(c);
  1. var c = a.getSprite(2,2);//通过纹理索引,生成一个可控制物件
  2. console.log(c);//GridSprite {relativeX:0,relativeY:0,.....}

LayaAir引擎——(五)的更多相关文章

  1. LayaAir引擎——(六)

    LayaAir引擎——TiledMap地图图块属性获取和进行墙壁碰撞检测 需要的软件: TiledMap LayaAir IDE 1.0.2版本 所画的地图: pass层: floor层: pass层 ...

  2. LayaAir引擎开发HTML5最简单教程(面向JS开发者)

    LayaAir引擎开发HTML5最简单教程(面向JS开发者) 一.总结 一句话总结:开发游戏还是得用游戏引擎来开发,其实很简单啦 切记:开发游戏还是得用游戏引擎来开发,其实很简单,引擎很多东西都帮你做 ...

  3. LayaAir引擎——(七)

    LayaAir引擎——人物控制TiledMap地图移动和墙壁检测 所需要的软件: LayaAir IDE 1.0.2版本 TiledMap 所需要的东西: 地图:53 * 32,(48*48) 人物: ...

  4. LayaAir引擎——(四)

    LayaAir引擎 TiledMap 使用 所需要的软件: Tiled地图编辑器 版本0.16.2 LayaAir IDE 所需要的图片:图块图片(1.png) 步骤1: 文件->新文件-> ...

  5. LayaAir引擎——(二)

    LayaAir引擎 -> 工具 -> 图集打包例子

  6. LayaAir引擎——(一)

    LayaAir是LayaBox推出的Html5游戏引擎,支持 ActionScript3.TypeScript.JavaScript,开源,并且商用免费.   LayaAir IDE 是一款使用Lay ...

  7. Javascript多线程引擎(五)

    Javascript多线程引擎(五)之异常处理 C语言没有提供一个像Java一样的异常处理机制, 这就带来了一个问题, 对于一个子函数中发生异常后, 需要在父函数调用子函数的位置进行Check, 如果 ...

  8. LayaAir引擎——(三)

    LyaAir引擎(JavaScript)实现图片的翻转一半 图片4.png位于bin/开场过渡 文件夹下,图片大小150*30(根据实际情况做调整) var button; var scale1 = ...

  9. c json实战引擎五 , 优化重构

    引言 scjson是一个小巧的纯c跨平台小巧引擎. 适用于替换老的cJSON引擎的场景. 数据结构和代码布局做了大量改进.优势体现在以下几个方面: 1) 跨平台 (window 10 + VS2017 ...

随机推荐

  1. jquery.fullPage.js全屏滚动插件教程演示

    css部分(此处需要导入jquery.fullPage.css) <style> .section { text-align: center; font: 50px "Micro ...

  2. Redis常用命令入门4:集合类型

    集合类型 之前我们已经介绍过了最基本的字符串类型.散列类型.列表类型,下面我们一起学习一下集合类型. 集合类型也是体现redis一个比较高价值的一个类型了.因为Redis的集合类型,所以我们可以很容易 ...

  3. 我的毕业设计——基于安卓和.NET的笔记本电脑远程控制系统

    手机端: 电脑端:    答辩完成后会开放代码.

  4. java selenium (九) 常见web UI 元素操作 及API使用

    本篇介绍我们如何利用selenium 来操作各种页面元素 阅读目录  链接(link) <div> <p>链接 link</p> <a href=" ...

  5. 无废话SharePoint入门教程四[创建SharePoint母版页]

    一.前言 文章成体系,如果有不明白的地方请查看前面的文章. 二.目录 1.创建HTML页面 2.将HTML文件转换为SharePoint母版页 3.在 SPD中修改母版页“PlaceHolderMai ...

  6. 安装 node-sass 的正确姿势

    SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass

  7. Fortran版MPI_barrier出现错误可能情况

    在Fortran中的MPI_开头函数都常有一个整数型的错误变量用以函数返回错误信息.如: call MPI_Barrier(MPI_COMM_WORLD,ierr) 在没有ierr参数时,程序可能会出 ...

  8. Android中的Context

    Context用来访问全局信息的接口,比如影城程序的资源.一些常用的组件都是继承自Context,目的就是方便的访问资源,比如Activity, Service.... 从Context访问本组件的资 ...

  9. C#机器视觉入门系列1-转化为灰度图&&3*3模糊

    这是我入门机器视觉的系列学习经验之开篇,本来想着依靠opencv快速实现一些功能,但是想了一下既然是学数学的,还是应该自己多算算,写一些自己理解的东西才好. 入门篇很简单,就只是实现了转化成灰度图以及 ...

  10. php防止外链导出的代码

    先收藏起来再说! URL跳转代码 1.代码: <? $url=$_GET["url"];header("Location:"."http://& ...