hii…i way trying to understand spawning and pooling concept so i worte a simple program
there is an error after first iteration…
:- attempt to call method ‘removeSelf’(a nil value)
what i am trying to do
:- create two objetcs and put them in a table (t)
:-create a variable and assign a random value from the object in it
:-make the variable visible
:-after it crosses the screen …make the variable empty and again put a random value in it and so on
here is my code
local physics=require("physics") physics.start() local enemy local t={} local ball=display.newCircle(0,0,20) ball:setFillColor(1) ball.isVisible=false table.insert(t,ball) local ball1=display.newCircle(0,0,20) ball1:setFillColor(0.25,0.25,0.60) table.insert(t,ball1) ball1.isVisible=false print(#t) local function update() enemysel() end function enemysel() if(enemy==nil)then enemy=t[math.random(#t)] enemy.isVisible=true enemy.x=300+display.screenOriginX enemy.y=400+display.screenOriginY end if(enemy~=nil)then if(enemy.x\>100)then enemy.x=enemy.x-3 else enemy:removeSelf() enemy=nil end end end timer.performWithDelay(1,update,-1);
https://www.youtube.com/watch?v=AdzG43DYWkY&feature=youtu.be
Hi. I personally think that Pooling (Object Caching) is a waste of effort. However some may disagree.
Having said that, you are trying to do the right thing. i.e. You want to test its effectiveness at amortizing object creation costs.
I have put together an example to help you test this. It comes with an SSK2 and non-SSK2 version. The prior is more useful, but you can at least get started if you don’t have SSK2.
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/02/pooling.zip
Note: The only thing that SSK2 provides for this example is the meters. Other than that, the example is pure-Corona.
In the example, I provide a test that runs in four different ways:
- No Pooling + No Physics
- Pooling + No Physics
- No Pooling + Physics
- Pooling + Physics
The test runs as follows:
- start and prep content build/destroy functions.
- Create a table to act as a proxy for the current ‘to build’ count.
- Use a transition.to() hack to start increasing that ‘to build’ count from 100 to 10,000.
- Starts increasing count after 1 second.
- Takes 60 seconds to increase from 100 to 10000
- Create label to show current ‘to build’ count.
- Start enterFrame Listener
- Every frame destroy all objects.
- Every frame create ‘to build’ number of objects using the current method (pooling or non-pooling)
Effectively, this example is trying to create and destroy:
- 60 * 100 == 6,000 objects per second initially
- 60 * 10,000 = 600,000 objects per second by the end of 61 seconds.
This slow ramp in number-of-objects-to-build-destroy lets us evaluate the point where FPS starts to degrade.
The results for me on my primary dev machine are:
- No Pooling + No Physics - FPS starts to decline at around 60 * 1600 = 96,000 objects per second.
- Pooling + No Physics - Same
- No Pooling + Physics - FPS starts to decline at around 60 * 580 = 34,800 objects per second.
- Pooling + Physics - FPS starts to decline at around 60 * 700 = 42,000 objects per second.
(_ Note: High-resolution versions of video are still processing. Low-res versions make it hard to read FPS counter _)
Hey @kumarchetan312 -
I’m pretty new to Corona, so I may be completely wrong here, but I think your problem lies in the
enemy:removeSelf()
I believe that is removing the selected ball from the table not just the screen, so once you’ve removed both or if the same one gets randomly called twice in a row, the next random table item is nil and can’t be removed. If you comment out that line I think it works as you intended, except that the ball remains at it’s final location on screen. For that I simply reset it to it’s original state:
enemy.isVisible=false enemy.x=300+display.screenOriginX
Again, I’m a noob to this stuff so sorry if I’m off base with this response, but I figured it couldn’t hurt to try 
Best of luck!
https://www.youtube.com/watch?v=AdzG43DYWkY&feature=youtu.be
Hi. I personally think that Pooling (Object Caching) is a waste of effort. However some may disagree.
Having said that, you are trying to do the right thing. i.e. You want to test its effectiveness at amortizing object creation costs.
I have put together an example to help you test this. It comes with an SSK2 and non-SSK2 version. The prior is more useful, but you can at least get started if you don’t have SSK2.
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/02/pooling.zip
Note: The only thing that SSK2 provides for this example is the meters. Other than that, the example is pure-Corona.
In the example, I provide a test that runs in four different ways:
- No Pooling + No Physics
- Pooling + No Physics
- No Pooling + Physics
- Pooling + Physics
The test runs as follows:
- start and prep content build/destroy functions.
- Create a table to act as a proxy for the current ‘to build’ count.
- Use a transition.to() hack to start increasing that ‘to build’ count from 100 to 10,000.
- Starts increasing count after 1 second.
- Takes 60 seconds to increase from 100 to 10000
- Create label to show current ‘to build’ count.
- Start enterFrame Listener
- Every frame destroy all objects.
- Every frame create ‘to build’ number of objects using the current method (pooling or non-pooling)
Effectively, this example is trying to create and destroy:
- 60 * 100 == 6,000 objects per second initially
- 60 * 10,000 = 600,000 objects per second by the end of 61 seconds.
This slow ramp in number-of-objects-to-build-destroy lets us evaluate the point where FPS starts to degrade.
The results for me on my primary dev machine are:
- No Pooling + No Physics - FPS starts to decline at around 60 * 1600 = 96,000 objects per second.
- Pooling + No Physics - Same
- No Pooling + Physics - FPS starts to decline at around 60 * 580 = 34,800 objects per second.
- Pooling + Physics - FPS starts to decline at around 60 * 700 = 42,000 objects per second.
(_ Note: High-resolution versions of video are still processing. Low-res versions make it hard to read FPS counter _)
Hey @kumarchetan312 -
I’m pretty new to Corona, so I may be completely wrong here, but I think your problem lies in the
enemy:removeSelf()
I believe that is removing the selected ball from the table not just the screen, so once you’ve removed both or if the same one gets randomly called twice in a row, the next random table item is nil and can’t be removed. If you comment out that line I think it works as you intended, except that the ball remains at it’s final location on screen. For that I simply reset it to it’s original state:
enemy.isVisible=false enemy.x=300+display.screenOriginX
Again, I’m a noob to this stuff so sorry if I’m off base with this response, but I figured it couldn’t hurt to try 
Best of luck!