SolitudeX Posted July 20, 2016 Share Posted July 20, 2016 for (var i = 0; i < enemies.length; i++) { var e = enemies.getChildAt(i); if (e.isTweening) { continue; } e.isTweening = true; var r = game.rnd.between(100, 200); var a = game.rnd.between(0, 2 * Math.PI); var x1 = e.x + r * Math.cos(a); var y1 = e.y + r * Math.sin(a); var tween = game.add.tween(e).to( { x: x1, y: y1 }, 10 * r, null, true, 0, 0, false ); tween.onComplete.add(function () { console.log("complete. e.isTweening:"+e.isTweening+" e.name:"+e.name); e.isTweening = false; }); } The code above is in update,I want all the enemies to move randomly. I added name and isTweening to Sprite.prototype. But the problem is only the last child of the group runs the onComplete function . In the log the e.name is always the last child of the group. What wrong wrong with my code? Thanks a lot for your answer~ Link to comment Share on other sites More sharing options...
SolitudeX Posted July 20, 2016 Author Share Posted July 20, 2016 All the enemies tween for the first time ,but then all stop except for the last one in the group. Link to comment Share on other sites More sharing options...
SolitudeX Posted July 20, 2016 Author Share Posted July 20, 2016 The problem is solved by add a parameter to the onComplete function and edit the param's isTweening. Link to comment Share on other sites More sharing options...
rich Posted July 20, 2016 Share Posted July 20, 2016 For anyone else wondering, this is a classic JavaScript scope problem. Link to comment Share on other sites More sharing options...
Recommended Posts