Jump to content

For loops and object pools


pdiddles03
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...