こちらのサイトのtiledmaploaderというプラグインを使用したいのですが、記載されているパッケージをダウンロードして指示通りに書き換えてもエラーが出力されます
エラーは以下のものです
TypeError: xml.getElementsByTagName(...)[0] is undefined tiledMapLoader.js:149:28
これはenchant.jsというゲーム制作用のフレームワークに付属しているサンプルコードを使用しております
簡単に解説すると、付属のサンプルコードでは、巨大な配列に元画像のタイルを格納していき作成しているのですが、此方のプラグインを使うことで、配列を使わずに、ペイントソフトのようにマップを作ることができ、そのままそれを読み込むことができます
詳しくはこちらの記事を参考にしていただきたいです
tilelMapLoader.jsというファイルがどのように動いているのか、全く読み解けないので、どなたかご助力いただけないでしょうか
よろしくお願いいたします
<index.html>
ブラウザ上にゲームを表示するファイルです
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <title>enchant</title> <script type="text/javascript" src="enchant.js"></script> <script type="text/javascript" src="ui.enchant.js"></script> <script type="text/javascript" src="game.js"></script> <script type="text/javascript" src="tiledMapLoader.js"></script> <script type="text/javascript"> //読み込むファイルを指定する tiled.files = ['map0.tmx']; //複数のファイルの時は //tiled.files = ['map0.tmx','map1.tmx','map2.tmx']; </script> <style type="text/css"> body { margin: 0; } </style> </head> <body> </body> </html> コード
<game.js>
ゲームを動かすメインのファイルです
enchant(); window.onload = function() { var game = new Game(320, 320); game.fps = 15; game.preload(tiled[0].image, 'chara0.gif'); game.onload = function() { var map = new Map(tiled[0].map.tileheight, tiled[0].map.tilewidth); map.image = game.assets[tiled[0].image]; map.loadData.apply(map, tiled[0].background); map.collisionData = tiled[0].collision; var foregroundMap = new Map(tiled[0].map.tileheight, tiled[0].map.tilewidth); foregroundMap.image = game.assets[tiled[0].image]; foregroundMap.loadData.apply(foregroundMap, tiled[0].foreground); var player = new Sprite(32, 32); player.x = 6 * 16 - 8; player.y = 10 * 16; var image = new Surface(96, 128); image.draw(game.assets['chara0.gif'], 0, 0, 96, 128, 0, 0, 96, 128); player.image = image; player.isMoving = false; player.direction = 0; player.walk = 1; player.addEventListener('enterframe', function() { this.frame = this.direction * 3 + this.walk; if (this.isMoving) { this.moveBy(this.vx, this.vy); if (!(game.frame % 3)) { this.walk++; this.walk %= 3; } if ((this.vx && (this.x-8) % 16 == 0) || (this.vy && this.y % 16 == 0)) { this.isMoving = false; this.walk = 1; } } else { this.vx = this.vy = 0; if (game.input.left) { this.direction = 1; this.vx = -4; } else if (game.input.right) { this.direction = 2; this.vx = 4; } else if (game.input.up) { this.direction = 3; this.vy = -4; } else if (game.input.down) { this.direction = 0; this.vy = 4; } if (this.vx || this.vy) { var x = this.x + (this.vx ? this.vx / Math.abs(this.vx) * 16 : 0) + 16; var y = this.y + (this.vy ? this.vy / Math.abs(this.vy) * 16 : 0) + 16; if (0 <= x && x < map.width && 0 <= y && y < map.height && !map.hitTest(x, y)) { this.isMoving = true; arguments.callee.call(this); } } } }); var stage = new Group(); stage.addChild(map); stage.addChild(player); stage.addChild(foregroundMap); game.rootScene.addChild(stage); var pad = new Pad(); pad.x = 0; pad.y = 220; game.rootScene.addChild(pad); game.rootScene.addEventListener('enterframe', function(e) { var x = Math.min((game.width - 16) / 2 - player.x, 0); var y = Math.min((game.height - 16) / 2 - player.y, 0); x = Math.max(game.width, x + map.width) - map.width; y = Math.max(game.height, y + map.height) - map.height; stage.x = x; stage.y = y; }); }; game.start(); };
map0.tmx
サイズ的に文字数制限に引っかかるためこちらのアップローダに公開しました
tiledMapLoaderも記載しようと思いましたが文字数制限に引っかかってしまうため、記載できませんでした
参考としては、一番上で紹介した記事またはこちらに個別でファイルが上がっています
何かありましたらご要望お願いします
お手数ですがよろしくお願いいたします
回答2件
あなたの回答
tips
プレビュー