Khaaaaaaaaaaaaaaaaaan!

Some problems deserve immediate attention and simply can’t wait… a swarm of angry bees in your living room, for example, must be dealt with promptly especially if one wishes to have sex on the sofa. Other problems, such as several slithering snakes sauntering around the garden, can be brushed under the carpet for the time being – just take a little extra care when BBQing for a while until a solution presents itself or you get around to it. And solutions presenting themselves in the fullness of time is exactly what seems to happen surprisingly often in software development.

Swarm Of Bees

Yes, I did just copy and paste the same poorly drawn bee several times

Take swiping-to-scroll on the iPhone. My game, the one that’ll never be finished, has a level selection screen. Whilst it looks a little like a soviet supermarket right now, I have grand plans for filling it with a smorgasbord of wonderful level choices across several oddly conceived themes. The idea is that you swipe backwards and forwards and get that sexy velocity type thingie where you let go with your finger and the whole thing keeps moving until you bring it to a juddering halt yourself or it just runs out of steam. It I had used standard Cocoa Touch classes and all that then all this would have been given to me as free functionality but I’m not, I’m using OpenGL ES.

I’ve sat on this issue for several months: with one level, it’s not exactly top-of-the-list when it comes to required functionality. Furthermore, it’s still possible to drag it left and right to pick levels, it just doesn’t have that really nice velocity thing when you let go of the screen. But, to use that sadly underused word that Khan applied so excellently in Star Trek 2, it vexed me. I’d put several hours into it here and there and all I seemed to be doing was creating increasingly more complex code that provided fewer and fewer features. It was obvious that there was a super-simple, amazingly easy answer, but for some odd reason I just couldn’t see it. In the end I decided to stop attempting to flog a dead horse, leave it and see what fell out of the woodwork.

Then, after a particularly good day a couple of weeks ago, a miracle occurred whilst I was in the bath (no, not that kind of miracle): a solution presented itself. As the user drags their finger across the screen (touchesMoved), I reset a timer and store the delta. When they let go (touchesEnded), I check the timer: if it’s less than a twentieth of a second or so then I set the scroll velocity to be the last movement delta (so long as the velocity is greater than a reasonable amount). Et voila! It works! And as an added bonus, it’s three lines of code and is nicely generalised in the entity manager. Now, if I can just add another level or two then it’ll all have been worth it. Needless to say, I duplicated my one and only lonely level three times so I could whizz the screen backwards and forwards, well, it amused me. Zooooooooooooooooooooooooooooooom!

This entry was posted in My iPhone game and tagged , , . Bookmark the permalink.

Comments are closed.