• LayaAir引擎——(七)


    LayaAir引擎——人物控制TiledMap地图移动和墙壁检测

    所需要的软件:

    LayaAir IDE 1.0.2版本

    TiledMap

    所需要的东西:

    地图:53 * 32,(48*48)

    人物:48*48

    注意:

    var a = pass.getTileDataByScreenPos(player.x,player.y - 48);//返回图块在图块集纹理上的索引+1

    562//实际上索引是561,判断地图上的格子是否是561,如果是,不可以通行,则反之。

    var Sprite    = Laya.Sprite;
    var Stage     = Laya.Stage;
    var MapLayer  = Laya.MapLayer;
    var TiledMap  = Laya.TiledMap;
    var Point     = Laya.Point;
    var Rectangle = Laya.Rectangle;
    var Handler   = Laya.Handler;
    var WebGL     = Laya.WebGL;
    
    Laya.init(624, 576);
    
    
    var map1 = new TiledMap();
    map1.createMap("map/map1/town1.json",new Rectangle(0,0,624,576),Handler.create(this,onMapLoaded));
    var pass;
    
    
    var player;
    var mX = 0;
    var mY = 0;
    
    function onMapLoaded() {
        pass = map1.getLayerByName("pass");
        
        player = new Sprite();
        player.loadImage("map/player/player.png",0,0,48,48);
        Laya.stage.addChild(player);
    
        Laya.stage.on(laya.events.Event.KEY_DOWN,this,onkeydown);
    }
    
    function onkeydown(e) {
        switch (e.keyCode) {
            case 38:{
                var a = pass.getTileDataByScreenPos(player.x,player.y - 48);
                if(a != 562){
    	            if(mY  > 0 ){
                        if (player.y > 288) {
                            player.y -= 48;
                        }else{
                             mY -= 48;
                     	    map1.moveViewPort(mX,mY);
                        }
    	               
    	            }else{
                        if (player.y > 0) {
                            player.y -= 48;
                        }
                    }
                }
                break;
            }
            case 40:{
                var a = pass.getTileDataByScreenPos(player.x,player.y + 48);
                if(a != 562){
                    if (player.y > 144) {
                        if (mY < 960) {
                            mY += 48;
                            map1.moveViewPort(mX,mY);
                        }else{
                            if (player.y < 528) {
                                player.y += 48;
                            }
                        }
                    }else{
                        player.y += 48;
                    }
                }
                break;
            }
            case 37:{
                var a = pass.getTileDataByScreenPos(player.x - 48,player.y);
                if(a != 562){
                   if(player.x > 288){
                       player.x -= 48;
                   }else{
                       if (mX > 0) {
                            mX -= 48;
                           map1.moveViewPort(mX,mY);
                       }else{
                           if (player.x > 0) {
                               player.x -= 48;
                           }
                       }
                   }
                }
                break;
            }
            case 39:{
                var a = pass.getTileDataByScreenPos(player.x + 48,player.y);
                if(a != 562){
                    if (player.x > 288) {
                        if (mX > 1872) {
                            if (player.x < 576) {
                                player.x += 48;
                            }
                        }else{
                            mX += 48;
                            map1.moveViewPort(mX,mY);
                        }
                    }else{
                        player.x += 48;
                    }
                }
                break;
            }
            default:
                break;
        }
    }
    

      

  • 相关阅读:
    Android调用第三方so
    九度oj题目1518:反转链表
    九度oj题目1014:排名
    九度oj题目1012:畅通工程
    九度oj题目1027:欧拉回路
    九度oj题目1348:数组中的逆序对
    九度oj题目1521:二叉树的镜像
    九度oj题目1385:重建二叉树
    poj 1577 Falling Leaves
    poj 1321 棋盘问题
  • 原文地址:https://www.cnblogs.com/FXYDBK/p/5693464.html
Copyright © 2020-2023  润新知