在我的游戏中,我有一个角色类:
var NPC = function(isometry,timer,canvas,tile,tileMap,scrollPosition,
grid,spritesheet, r1, c1, currentHero, hh, hw,index) {
this.index = index;
....
NPC.prototype.w = new Worker('astar.js');
this.w.onmessage = function(e) {
//process worker results, 'index' is strongly needed
// e is data from worker
//it draws path for the character on the common tileMap:
tileMap[e.data[i].x][e.data[i].y] = index;
}
然后在主程序中创建该类的两个对象:
var protagonist = new NPC(.....,0);
var char1 = new NPC(.....,10);
问题是他们用相同的索引= 10绘制路径。我试过:
- 改为使用 this.index,但它在 this.w.onmessage 中变为未定义
- 称之为“NPC.prototype.w.onmessage = function(e) {”——同样的故事
声明单独的函数:
NPC.prototype.wonmessage=function(e) {....}
this.w.onmessage = this.wonmessage();
Error: "e is undefined".
我应该怎么做才能将所有数据正确发送到函数?