Load TMX from server?

Hi how to load tiled map from server? thanks Chinese community :slightly_smiling:

You can use cc.loader.getXMLHttpRequest() to retrieve a xhr object, then use it to download the content of your tmx file, then, you will also need to load your images related using cc.loader.load

1赞

@eydamson
You can implement it by create a cc.TiledMapAsset object with your tmx file data. But there is a limitation: the image & tsx files used by the tmx file should be in the assets of your project. The logic will be look like this:

// loadFileFromServer should be replaced by actual method which can load file data from server.
var mapNode = this.node;
loadFileFromServer(TMX_URL, function (err, data) {
    var tmxAsset = new cc.TiledMapAsset();
    tmxAsset.tmxXmlStr = data;

    // RELATIVE_PATH_OF_IMAGES is a relative path to the assets folder.
    tmxAsset.tmxFolderPath = RELATIVE_PATH_OF_IMAGES;

    // IMAGE_NAMES is a array which contains all the image files name used by tmx.
    var imgPaths = IMAGE_NAMES.map(function(imgName) {
        return RELATIVE_PATH_OF_IMAGES + '/' + imgName;
    });
    cc.loader.load(imgPaths, function() {
        var map = mapNode.addComponent(cc.TiledMap);
        map.tmxAsset = tmxAsset;
    });
});

Hope it’s helpful!

1赞

@natural-law thanks…
It works… but can we make it where our tmx image resources can be also on the server?..

or if i load my images from the server how do i set the tmxFolderPath to where the path of my image resources are being loaded?.. hehe sorry for the trouble… :sweat_smile:

大神你好,请问这是图块图片的网址吗?我照着这个来做了,但是显示图片找不到

求大神解答一下,谢谢!