Azumi Posted September 14, 2016 Share Posted September 14, 2016 Hello. When I use forEachAlive loop on a group I see that it goes in ascending index order, I need to loop through group children in reverse order without changing their indexes or display order. I searched on google, in documentation ad in examples but I can't find a solution. I will be very grateful for any help. Link to comment Share on other sites More sharing options...
samme Posted September 14, 2016 Share Posted September 14, 2016 var child, i; i = group.children.length; while (i--) { child = group.children[i]; // … } Azumi 1 Link to comment Share on other sites More sharing options...
Azumi Posted September 15, 2016 Author Share Posted September 15, 2016 Thank you for replying. From what I understand, this loop would get me all children, not only the ones that are alive? Link to comment Share on other sites More sharing options...
Milton Posted September 15, 2016 Share Posted September 15, 2016 Yes, but the next step would be 'if (child.alive) {...}'. Azumi 1 Link to comment Share on other sites More sharing options...
Azumi Posted September 15, 2016 Author Share Posted September 15, 2016 Thanks for all the help. I looked into source code of the framework and added changed versions of functions that already exists there. I changed the "for" loop in iterate function. It probably is a little bit of an overkill but it works fine so far. Phaser.Group.prototype.reverseIterate = function (key, value, returnType, callback, callbackContext, args) { if (this.children.length === 0) { if (returnType === Phaser.Group.RETURN_TOTAL) { return 0; } else if (returnType === Phaser.Group.RETURN_ALL) { return []; } } var total = 0; if (returnType === Phaser.Group.RETURN_ALL) { var output = []; } for (var i = this.children.length - 1; i >= 0 ; i--) { if (this.children[i][key] === value) { total++; if (callback) { if (args) { args[0] = this.children[i]; callback.apply(callbackContext, args); } else { callback.call(callbackContext, this.children[i]); } } if (returnType === Phaser.Group.RETURN_CHILD) { return this.children[i]; } else if (returnType === Phaser.Group.RETURN_ALL) { output.push(this.children[i]); } } } if (returnType === Phaser.Group.RETURN_TOTAL) { return total; } else if (returnType === Phaser.Group.RETURN_ALL) { return output; } else { // RETURN_CHILD or RETURN_NONE return null; } }; Phaser.Group.prototype.forEachAliveReverse = function (callback, callbackContext) { var args; if (arguments.length > 2) { args = [null]; for (var i = 2; i < arguments.length; i++) { args.push(arguments[i]); } } this.reverseIterate('alive', true, Phaser.Group.RETURN_TOTAL, callback, callbackContext, args); }; Link to comment Share on other sites More sharing options...
Milton Posted September 15, 2016 Share Posted September 15, 2016 Nice work It would be better to have a ASC/DESC flag in the original, but whatever works... Azumi 1 Link to comment Share on other sites More sharing options...
Azumi Posted September 15, 2016 Author Share Posted September 15, 2016 Thanks! I didn't want to change the original "phaser.js" file in case of future upgrade so i just added these two in my "game.js" But in my opinion this kind of functionality should be implemented in stock phaser. Link to comment Share on other sites More sharing options...
Recommended Posts