最近在做一款微信小游戏,参考了官方的暗黑斩示例,里面有一个定时器的需求,我使用的this.scheduleOnce(this.spawnFoe(spawn), spawn.outTime);这个定时器,因为我需要执行的方法里需要传一个参数,但是这样写会报错:Uncaught Error: callback function must be non-null,如果我不传参数this.scheduleOnce(this.spawnFoe, spawn.outTime);这样就不会报错。但我确实是需要往方法里传一个参数的,后面我改成了setTimeout(this.spawnFoe(spawn), spawn.outTime);这样不会有问题,但是官方是推荐使用计时器,跟组件结合得更好,而且确实功能要强大一些,所以想请教一下,为什么this.scheduleOnce(this.spawnFoe(spawn), spawn.outTime);方法里面传了参数会报错,有没有什么解决办法?下面是全部的代码,完全参考的暗黑斩,只不过根据自己的业务做了一些修改。
startSpawn () {
for(var i=0;i<this.currentWave.spawns.length;i++){
var spawn=this.currentWave.spawns[i];
this.scheduleOnce(this.spawnFoe(spawn), spawn.outTime);
// setTimeout(this.spawnFoe(spawn), spawn.outTime);
}
},
spawnFoe:function (spawn) {
this.currentSpawn = spawn;
for(var i=0;i<spawn.total;i++){
let newFoe = spawn.spawn(this.game.poolMng);
if (newFoe) {
this.foeGroup.addChild(newFoe);
// newFoe.setPosition(this.getNewFoePosition());
newFoe.getComponent(‘monster’).init(this);
}
}
},
因为你参数传的不对,看api
我看过官方手册和API文档的,好像都没有相关的示例,而且官方的API文档真是太难用了。。。朋友如果知道的话,麻烦说一下好吗?感谢!
this.scheduleOnce(function(){
this.spawnFoe(spawn)
}, spawn.outTime);
原来要这样,谢了兄弟!
计时器 建议使用 setInterval和setTimeOut实现 比schedule系列要方便很多
或者可以用 action