onaluf / gameQuery

a javascript game engine with jQuery
http://gamequeryjs.com
633 stars 161 forks source link

some sprites are not cleared #17

Closed ex closed 11 years ago

ex commented 11 years ago

I just checked out the master tutorial demo and saw one bullet sprite not being cleared and floating on the game sreen, I saw the same bug in the online demo. Is this a know bug? It seems to happen frequently.

onaluf commented 11 years ago

Hi, yes this is a known bug. The problem is that I use this to generate a unique id for missiles: Math.ceil(Math.random()*1000)

This turns out to return the same value from time to time and two sprite are generated with the same ID. This lead to the problem you describe.

The solution is very simple: use a revolving counter instead:

var counter = 0; ... counter = (counter + 1) % maxCounterValue

I will commit this change when I find the time!