In my game engine I have an animation class that maintains a set of listeners and broadcasts events to them when certain animation states occur, namely animation start, animation update, and animation end. The engine maintains an array list of active animations that get updated every game tick which in turn trigger the related events. In my particular instance, there is a button in the engine which creates a new window and an animation that’s used to control the widow’s position. The animation is assigned an anonymous listener that handles the events and updates the position accordingly. Finally the animation is started and added to the list of active animations. So far so good.
Now the question. When the animation class broadcasts the animation end event, I would like to be able to remove it from the list of active animations. Of course if I try to remove the animation from the “active list” from within the “animation end” event handler, I get a concurrent update exception. What would be the proper way to handle this situation? So far the only solution that I’ve thought of would involve marking the animation as “dead” and having the master animation list remove it (or any other dead animations) on the next iteration through the list. I have a nagging feeling that there is probably a better solution to this which is eluding me at the moment. Does anyone have any advice on how to approach this, or is the “mark and sweep” method the only way? ???