pdiddles03 Posted November 16, 2015 Share Posted November 16, 2015 Hey All, I have a game that I am using object pools for. I have all of them stored in an array that i retrieve by a function that shifts one out of the array and I grab it and use it. I put the ones I grab into an array and i loop through them. My problem is when I perform something like collision on it and use array.splice('a number', 1) to remove it and return the one removed, I can't get it to actually skip that one on it's next loop, it goes through and tries to insert the same one into the object pool. Anybody help please? Quote Link to comment Share on other sites More sharing options...
bubamara Posted November 16, 2015 Share Posted November 16, 2015 sprites with alpha equal to zero or with visible property set to false are not rendered. make a pool array, set all sprites visible to false and add them to stage/container.when you will need one, iterate through pool array, grab first with invisible and make it visible and break the loop. repeat as many times as needed. getAvailablePoolSprite = function() { for (var i=0; i < this.maxPoolSprites; i++) { if (this._pool[i].visible === false) { this._pool[i].visible = true; break; } } if (i === this.maxPoolSprites) { // out of pool sprites return null; } return this._pool[i];}do collision check only with visible sprites and when you don't need them anymore, make them invisible again Quote Link to comment Share on other sites More sharing options...
CtlAltDel Posted November 17, 2015 Share Posted November 17, 2015 It is probably faster to use 2 pools, 1 visible and 1 invisible if you have lots of sprites, then you can just pop one from the invisible list and add it to the visible list. Keep in mind this requires you to toggle the visible flag via a helper function that does the moving of sprite between pools. Quote Link to comment Share on other sites More sharing options...
bubamara Posted November 17, 2015 Share Posted November 17, 2015 It is probably faster to use 2 pools, 1 visible and 1 invisible if you have lots of sprites, then you can just pop one from the invisible list and add it to the visible list. Keep in mind this requires you to toggle the visible flag via a helper function that does the moving of sprite between pools. I'm pretty sure that setting visible property to true/false is faster than adding/removing children to/from container Quote Link to comment Share on other sites More sharing options...
CtlAltDel Posted November 17, 2015 Share Posted November 17, 2015 It's looking up the next invisible sprite that might be costly if you have a lot of sprites. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.