Looping through Particles in Android
Can anyone please help?
I'm doing a small Android game where I have an emitter emitting a single particle in a random direction each tick of the game. I have a simple emitter, particle and main game class.
In my main game class I declare an array of e.g 100 particles. My problem is that I use a for loop to loop through the array of particles each game tick to update that particles state. My problem is however, that the for loop loops through all the particles in one go each tick and all the particles are updated simultaneously, and therefore drawn on top of each other,开发者_运维问答 giving the illusion that there is only 1 particle on the screen. I have tried different loop structures but with no luck.
Can anyone please suggest a solution or point me to a tutorial that might help me with this issue. I can post some code if necessary.
Thanks in advance.
Here's a thought: How about adding a member "time" variable to each particle. Now, when you initialize the particles (lets say using a for loop), give each particle a birth-time value of -i. so:
particle[0].time : 0
particle[1].time : -1
particle[2].time : -2
particle[3].time : -3
particle[i].time : -i
Now, when you update each particle at every tick in your update function, you'd update:
particle[i].time += 1
This way, particles are gradually "born" (t >= 0)
In your draw, you can put a simple check:
if (particle[i].time >= 0) // particle has been born
// draw particle
Hope this helps.
Also, I recommend moving this to: https://gamedev.stackexchange.com/
精彩评论