Hi,
what is the best method, have one enter_frame and put all the enemies in a table and make a "for" or add to each enemie an enter_frame event and remove it when the enemie is destroyed?
the same question for bullets, players, bullet players, explosion, etc
sorry for my english.
Maxi.
Comments
Likes: SinisterSoft
https://en.wikipedia.org/wiki/Object_pool_pattern
And here is a thread on the forums about pooled objects..
http://giderosmobile.com/forum/discussion/4769/simple-item-pool-class/p1
Likes: SinisterSoft
https://deluxepixel.com
https://deluxepixel.com
Pool class:
Regards.
Maxi.
It all depends on what it does: when do you need to call it? if it's something that happens on every frame you usually call it with onEnterFrame event (as sinisterSoft said, better to have just one enterframe listener, where you call every enterframe function). If it's used to "reset" your enemie stats, you can call it when you destroy the object, or when you get it out from the pool.
Pool is just a "storage" where you place your enemie(s) instances, instead of garbage collecting them, to reuse them later.
You will need to reset anything that should be "new" in createObject - for example hitpoints.
To create different objects you need to "prepare" your pool so it is able to decide which object it needs to create. You must also tell it which kind of object it is when you destroy it.
Haven't tested it, but it should be something like this:
hope this helps
thanks for you reply, every enemi needs to move or check if it collides with a bullet, so, I plan put this code in a function in Enemie class: example
You understand me?
sorry for my english.
Maxi.
it should be something like this (not tested)
another option could be to save the id number of each object (powerup and enemie) and replace every removed object in actors table with something that tells your loop to bypass the value. Of course you will have to read each object id to replace the right "dead" object in your actors table.
ie. object id 3 is destroyed, so actors[3] = "dead": in enterframe check if it's available or not.
I need make global thepool class to call function Pool:destroyObject(b, what) from enemie class?
so in update from enemie class i can make thepool:destroyObject(self, "enemie") and need remove it from actors table?
I need set nil actor tables and thepool class for example when change the scene or restart the level?
you don't need to make thepool global if you create/destroy your "actors" in enterframe: for example you could just set a property inside your enemie class to determine what should enterframe do with each object
ie:
if I use table.remove (), I do not need "if not a ==" dead "then" because using table.remove the object is deleted and the table is rearranged? what is best method use table.remove for actors table or set "dead" an actor and not call update if dead.
other question:
if I use actors = nil and thepool = nill I remove all memory tables?
sorry for the questions storm and english, I appreciate your help.
If you just nil the value of actor[i] it's almost the same (as long as you use pairs to parse actors - pairs doesn't bother about consecutive indices) but it should be faster since it doesn't need to rearrange the table.
In this scenario you don't need to set each "dead" object, but you will need to keep track on how many enemies/powerups you have already spawned to avoid overwriting existing ones (you need to provide unique id for each object, or you risk that some object is not updated by enterframe).
Be aware that lenght operator # works until it finds a "hole" between indices, so you need a variable to store "lastIdNumber".
what is best method use table.remove for actors table or set "dead" an actor and not call update if dead.
This really depends on your game: if you have a limited number of enemies that does not respawn/ respawn until they are n on screen you could go for the first option, the other is good if your enemies number may vary in time, but it's a bit more "complex" to set up because you need to have unique ids (as said before).
if I use actors = nil and thepool = nill I remove all memory tables?
Yes, as soon as garbage collector does its job
I am using main, pool and enemie class. I can use this method to create dozens of bullets for example if my car shoots? I will create a new table like enemies in main for example for bullets or power ups etc to check collisions for example.
Here is a link to what I'm working on now btw as a side project to test out new Gidero features...
https://deluxepixel.com
wow Bacteria 2 look awesome and the performance is really good.
Likes: SinisterSoft
You can use the same method to create dozens of bullets, why not?
however, if I understood how the game would be, I would leave the car in the center of the screen, able to move only on the y axis to avoid obstacles and move the background on the x.
I need change the code, becose #enemies was always 0
Likes: pie
https://deluxepixel.com
Maybe you could insert everything in a scene using scenemanager, so you can easily add a menu and "reset" your level as needed. Right now everything is on stage, which is not "wrong", but since you are using classes I think it would help you later in the development.
Likes: totebo, antix