码迷,mamicode.com
首页 > 其他好文 > 详细

LayaAir引擎——(七)

时间:2016-07-21 23:45:51      阅读:598      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
}

  

LayaAir引擎——(七)

标签:

原文地址:http://www.cnblogs.com/FXYDBK/p/5693464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!