zoukankan      html  css  js  c++  java
  • LayaAir引擎——(六)

    LayaAir引擎——TiledMap地图图块属性获取和进行墙壁碰撞检测

    需要的软件:

    TiledMap

    LayaAir IDE 1.0.2版本

    所画的地图:

    pass层:

    floor层:

    pass层格子属性:

    白色格子: id:48,自定义属性 isCanPass:true

    黑色格子:id:44,自定义属性 isCanPass:false

    floor层格子属性

    五芒星格子:id:0

    石头格子:id:27

    矿车格子:id:22

    1.前提代码

    Laya.init(576, 576);
    	
    var player;
    var obj;
    var floor;
    var pass;
    
    var map1 = new TiledMap();
    map1.createMap("map/map1/town1.json",new Rectangle(0,0,576, 576),Handler.create(this,onMapLoaded));
    
    function onMapLoaded(){
         pass = map1.getLayerByIndex(0);//获取通行层
     
         player = new Sprite();
         player.loadImage("map/map1/player.png",0,0,48,48);
         Laya.stage.addChild(player);
    
         Laya.stage.on(laya.events.Event.KEY_DOWN,this,onKeyDown);//设置键盘监听事件
    }
    

    2.重点代码

    function onKeyDown(e) {
      switch (e.keyCode) {
        case 38:{
          if ( (player.y - 48) <= 0) {
            player.y = 0;
          }else{
                 var a = pass.getTileData(player.x / 48, (player.y - 48)/ 48);
                 var b = map1.getTileProperties(0, a-1, "isCanPass");
                 if(b){
              player.y -= 48;
            }
              }
          break;
          }
          case 40:{
              if ( (player.y + 48) >= (576- 48)){
            player.y = 576 - 48;
              }else{
                 var a = pass.getTileData(player.x / 48, (player.y + 48)/ 48);
                 var b = map1.getTileProperties(0, a-1, "isCanPass");
                 if(b){
              player.y += 48;
            }
              }
              break;
          }   
          case 37:{
          if ( (player.x - 48) <= 0) {
            player.x = 0;
          }else{
            var a = pass.getTileData( (player.x - 48)/ 48,player.y/ 48);
            var b = map1.getTileProperties(0, a-1, "isCanPass");
            if(b){
               player.x -= 48;
            }
          }
          break;
          }
          case 39:{
          if ( (player.x + 48) >= (576 - 48)) {
            player.x = 576 - 48;       }else{         var a = pass.getTileData( (player.x + 48)/ 48,player.y/ 48);         var b = map1.getTileProperties(0, a-1, "isCanPass");         if(b){           player.x += 48;         }       }
        break;
        }  
      } }

     

    3.其中代码重点

    MapLayer.getTileData(x,y)

    参数:

    x:格子在地图上的x坐标,等同于屏幕坐标/格子的宽度

    y:格子在地图上的y坐标,等同于屏幕坐标/格子的高度

    返回:格子在纹理图块上的id值+1

    例子:

     var a = pass.getTileData( 0, 0);
     console.log(a);//49(白色格子ID:48)
    

    TiledMap.getTileProperties(textureIndex,tileIndex,propertyName);

    参数:

    textureIndex:格子所在的纹理图块的索引

    tileIndex:格子在纹理图块上的索引

    propertyName:自定义属性的名称

    返回:属性内容

    例子:

    var b = map1.getTileProperties(0, 44, "isCanPass");
    console.log(b);//false(黑色格子)
    

      

  • 相关阅读:
    强连通分量
    mysql安装图解 mysql图文安装教程(详细说明)
    MeshCombineUtility.cs method `GetTriangleStrip' of type `UnityEngine.Mesh' could be found
    socket网络编程之一:客户端和服务器端接口函数
    unity3D——登录界面
    Ionic学习注意事项01
    这里我将分享我学到的几种ionic登陆界面
    关于ionic基础教程的学习网址
    Windows下配置Git
    Cocos网络篇[3.2](3) ——Socket连接(1)
  • 原文地址:https://www.cnblogs.com/FXYDBK/p/5689919.html
Copyright © 2011-2022 走看看