Dealing with Touch Events, the Sparrow way.

This is specifically caught me, when I am coming from other object oriented technology. May be because of those environments are all single touch based. And I never have worked in multi-touch based application.
While working in Sparrow-framework, there is a nice event called “SP_EVENT_TYPE_TOUCH” . This is basically used if we want to use the touch based interaction in our game. Once we add an handler to it, the general expectation is it will just fire up once per touch/tap. But actually it will fire up 2 times as we have 2 different events, “touchesBegan” and “touchesEnded” for each touch interaction in the main cocoa/objectiveC framework. And the good news is Sparrow-framework does a nice job of encapsulating the internals of all those two events and gives us one event to deal with, that is “SP_EVENT_TYPE_TOUCH”.
Well, but the catch is getting confused with the event handler code. Lets dive in here.

[self addEventListener:@selector(onUserTouch:) 
	atObject:self forType:SP_EVENT_TYPE_TOUCH];

Now the handler code will look as below

- (void)onUserTouch:(SPTouchEvent*)event
{
        NSLog(@"onUserTouch");
}

When run, this code will generate log messages two times for a single touch! Thats for the reason we discussed earlier. Well, the fix is on the handler. We have to deal the events inside if-else where we will check, whether its single or multi touch as below.

- (void)onUserTouch:(SPTouchEvent*)event
{
        NSLog(@"onUserTouch");
        NSArray *touches = [[event touchesWithTarget:self 
                       andPhase:SPTouchPhaseBegan] allObjects];
	if (touches.count==1) {
		NSLog(@"1 fingure touch ");
               //Do the custom code here for single touch events
	}else if (touches.count==2) {
		NSLog(@"2 fingure touch ");
	}
}

For only single touch the syntax is even smaller

- (void)onUserTouch:(SPTouchEvent*)event
 {
     SPTouch *touch = [[event touchesWithTarget:self 
                 andPhase:SPTouchPhaseBegan] anyObject];
     if (touch)
     {
         SPPoint *touchPosition = [touch locationInSpace:self];
         NSLog(@"Touched position (%f, %f)", 
              touchPosition.x, touchPosition.y);
     }
 }

The only difference being, while handling single touches we can directly use “anyObject” as the last parameter and that will give us a single event and for multi-touch environments we have to use “allObjects” as the last parameter. But all this will happen inside the event handler, which will by default fire up twice.
One has to get the “touch” or “touches” inside the event handler and then process the code according to it. Directly the handler will fire up twice and that must not confuse as to, whats happening around!

Happy coding :)

My opensource iphone game now supports accelerometer

I have made some improvements since my last post about the game. The game (Math Is Fun 2) now supports accelerometer. The new version of the game is having below mentioned additions and updations to it.

1. Accelerometer support ( The game mode is now changed by changing phone’s direction )
2. A counter is added ( Which changes question in every 1000 count )
3. Game now displays, total questions played ( asked )
4. Total number of correct answers given by user is displayed
5. Total number of wrong answers given by user is displayed

The code is on GitHub and licensed under Unlicense.