English 中文(简体)
阅读JSON 地图编辑档案和显示信库
原标题:Reading JSON Tiled map editor file and displaying to canvas

http://blog.hashrocket.com/posts/using-tiled-and-canvas-to-render-amines” rel=“noreferer”>this tutorial to be found toload jsonmap documentation produced by tiledmap Editor in my javascript/canvashoo.

只字不提我自己使用过哪一种版本,在 con或净等的火力上没有错误。

只要看一看,就把ole塞。 标识和警示,文字绝对是罚款!

The problem is the canvas stays blank! when it should have a tilemap now on it.

Here is my version of the tutorial implemented in my game:

function Level() {
var c;
var data;
var layers = [];

this.get_map = function(name,ctx){
    c = ctx;
    $.getJSON( maps/ + name +  .json , function(json){
    get_tileset(json);
    });
};

function get_tileset(json) {
    data = json;
    this.tileset = $("<img />", { src: json.tilesets[0].image })[0];
    this.tileset.onload = renderLayers(this);
}

function renderLayers(layers){
    layers = $.isArray(layers) ? layers : data.layers;
    layers.forEach(renderLayer);
}

function renderLayer (layer){
    if (layer.type !== "tilelayer" || !layer.opacity) {
        alert("Not a tileLayer");
    }
    var s = c.canvas.cloneNode(),
            size = data.tileWidth;
    s = s.getContext("2d");

    if (layers.length < data.layers.length) {
        layer.data.forEach(function(tile_idx, i) {
            if (!tile_idx) { return; }
            var img_x, img_y, s_x, s_y,
                tile = data.tilesets[0];
            tile_idx--;
            img_x = (tile_idx % (tile.imagewidth / size)) * size;
            img_y = ~~(tile_idx / (tile.imagewidth / size)) * size;
            s_x = (i % layer.width) * size;
            s_y = ~~(i / layer.width) * size;
            s.drawImage(this.tileset, img_x, img_y, size, size,
                s_x, s_y, size, size);
        });

        layers.push(s.canvas.toDataURL());
        c.drawImage(s.canvas, 0, 0);
    }
    else {
        layers.forEach(function(src) {
            var i = $("<img />", { src: src })[0];
            c.drawImage(i, 0, 0);
        });
    }

}

}

并且从我的主要 j手档案中传来:

$(document).ready(function(){

var canvas = document.getElementById("TBG");
var ctx = canvas.getContext("2d");

var ui = new Gui();
var level = new Level();

//----------Login/Register Gui --------------
$( #TBG ).hide();
$( #load-new ).hide();
$( #reg ).hide();
$( #login ).hide();

//if login_but is clicked do ui.login function
$( #login_but ).click(ui.login);
//if reg_but is clicked do ui.register function
$( #reg_but ).click(ui.register);

$( #new_but ).click(function(){
    game_settings("new");
});

$( #load_but ).click(function(){
    game_settings("load");
});

//if login_sumbit button is clicked do ui.login_ajax function
$("#login_submit").click(ui.login_ajax);

$("#reg_submit").click(ui.register_ajax);

$("#welcome").on("click", "#logout_but", ui.logout);

//________________________

//Initialisation of game

function game_settings(state){
    if(state == "load"){
        ui.load_game();
        //do ajax call to load user last save
        level.get_map("level_01",ctx);
    }
    else{
        //set beginning params


        //Change screens
        ui.new_game();
        alert("new game");
    }
}

// End Loop ------------------------------------------------------





});

I don t suppose you lovely people could spot why the tile-map isn t being printed to my canvas?

Thanks for any help Tom

问题回答

Tiled + Canvas

我在。 Shane Riley。 一个有趣的职位!

好消息 我从他的神学中挑选了他的代码,我有他在当地使用的手法。

在推进这一进程和审议你的法典时,我认为你可以通过照顾2个问题来制定自己的法典:

(1) 您的功用不大。

2) 你需要把Shane's demo案卷的all上点到位于your的地方计算机上。 我只是把所有这些档案集中起来(为我工作)。 你们需要联系这些档案(细节如下):

  • mountain.html
  • mountain.json
  • mountain.tmx
  • mountain_landscape_23.png
  • render_scene.js

Here are the details. These worked for me and they should work for you. But if not, let me know and I can post my complete code.

页: 1 在您的原封不动中,旋涡轮机预计可使用代号<>或在线功能,而不是功能要求。

// not this.tileset.onload=renderLayers(this)
this.tileset.onload=renderLayers;    

// change the signature of renderLayers 
// (you have “layers” in scope for visibility in this function so this s/b ok)
// So: not function renderLayers(layers)
function renderLayers()    

Please include an error catcher in your $.getJSON so you get visibility on failed requests!

$.getJSON( maps/ + name +  .json , function(json){
        get_tileset(json);
}).fail( alert(“I think you should know that something has gone horribly wrong!”);  );

Here are the changes required to localize your files.

在山区:html

    <script src="render_scene.js" type="text/javascript"></script>

中文译本

load: function(name) {
  return $.ajax({
    url: "mountain.json",
    dataType: "text json"
  }).done($.proxy(this.loadTileset, this));
}

In mountain.json:

"image":"mountain_landscape_23.png",

在山区:

<image source="mountain_landscape_23.png" width="512" height="512"/>

Mountain_landscape_23.png

重要! 视你如何建立自己的发展环境而定,你可能会获得一个多面的安保管理者,浏览器将拒绝抽出你的tile。 如果是的话,将这一笔记卷带入像光店这样的编辑中,将其撤回到贵方的田地,以抵消CORS的错误。

Heh, 约10yrs迟,但如果你改变,

        size = data.tileWidth;

纽约总部

        size = data.tilewidth;

它完美运作。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.