您也可以像这样在 preload 函数中加载插件
this.load.scenePlugin({
key: 'PhaserNavMeshPlugin',
url: PhaserNavMeshPlugin,
sceneKey: 'navMeshPlugin'
});
这里是完成后带有迷你路径跟踪的演示代码:
var config = {
scene: {
preload,
create,
}
};
var game = new Phaser.Game(config);
function preload() {
this.load.image('tile01', 'tile01.png');
this.load.scenePlugin({
key: 'PhaserNavMeshPlugin',
url: PhaserNavMeshPlugin,
sceneKey: 'navMeshPlugin'
});
}
function create() {
var map = this.make.tilemap({ data: [[0,1,0],[0,1,0],[0,1,0],[0,0,0]], tileWidth: 8, tileHeight: 8 });
var tiles = map.addTilesetImage('tile01');
var layer = map.createLayer(0, tiles, 0, 0);
layer.setCollision(1);
const navMesh = this.navMeshPlugin.buildMeshFromTilemap("mesh", map, [layer]);
const path = navMesh.findPath({ x: 4, y: 4 }, { x: 17, y: 4 });
if (!path) {
return;
}
let graphics = this.add.graphics();
graphics.lineStyle(2, 0xff0000, 1);
graphics.beginPath();
graphics.moveTo(path[0].x, path[0].y);
for (let idx = 1; idx < path.length; idx++) {
graphics.lineTo(path[idx].x, path[idx].y);
}
graphics.strokePath();
}