objective-cioscocoa-touchsparrow-framework

IOS Sparrow Framework: how to prevent double touch


I made a "game over" text appears at the end of the game and added addeventlistener to the self.stage on touch. when this touched, it should execute the event function which loads the first screen of the game. however, on the first screen I have in the middle of the screen click to play text which has its own touch event.

In the simulator, if I click in the middle of the screen on the game over screen, it automatically also record event for click to play and the game starts.

-I don't think the eventhandler of click to play sticks around, because I use [self removeAllChildren] when game starts. also, I tested during game play by clicking in the same area and correctly no touch event records for click to play.

Edit:

I made a workaround for this, but not sure if that's an optimal solution:

I made the onevent touch start after an interval of time using NSTimer

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(attachEventToStart:) userInfo:startTextField repeats:NO];

then in attachEventToStart:

-(void)attachEventToStart:(NSTimer *)theTimer
{
    SPTextField *startTextField = [theTimer userInfo];
    [startTextField addEventListener:@selector(gameStartOnTouch:) atObject:self forType:SP_EVENT_TYPE_TOUCH];
}

this worked, if anyone have a better solution would be great


Solution

  • I found better alternative than the timer.

    I need to use SPTouchPhaseEnded , so I added the following lines to the event callback,

    SPTouch *touchEnded = [[event touchesWithTarget:self andPhase:SPTouchPhaseEnded] anyObject];
    if (!touchEnded)
         return;
    SOME GAME START CODE HERE...
    

    Now the action only happens when touch event ends, therefore it will not be carried out to the next screen. so this works like on key_up for those who are familiar with html-JS or Visual studio

    Make sure the callback parameter is of type SPTouchEvent not SPEvent.

    I found the answer here: http://forum.sparrow-framework.org/topic/event-listeners-getting-called-too-fast