Initialize. I said Initialize!

So I had some odd behaviour happening with some user interface code I’ve been toying around with. When I was clicking on an on-screen button it was calling the click event twice. I had no idea why it was doing this, so I followed through the call stack, only to see that things were properly being called in order (just in case I was somehow calling Update twice or something). It all looked fine. I ran through the call stack again, and it still all looked fine. I eventually stepped through and stumbled across something – I had called Initialize() twice on my GameScreen class, which was causing two identical buttons to be added, and both were firing their click event. I was creating a new GameScreen and calling Initialize() on it, then adding it to the GameScreen Manager, which also then called Initialize() on it. So, everything was working exactly as it should, just doubly so.

So, how does one prevent just such a thing from happening? Have a flag on the GameScreen class to ask if it’s been initialize? Specify some sort of access modifier on the Initialize() method? Learn lessons from making a mistake like this, then ‘Don’t Do It Again’? You tell me.

On a side note, the video blog from Gas Powered Games – Kings and Castles is entertaining. Kudos to them for being open like that.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s