Search code examples
iosiphonecocoa-touch

The device is bombarded by touchesEnded calls


I have a simple OpenGL:ES application running (it is a game). The game loads and presents the user with a "new game button" and then you are in the game. I'm using touchesBegan / touchesEnded to handle the touches. Then I take the coordinates and process them accordingly.

I also have an NSTimer running at 30Hz that calls renderScene which draws the on screen graphics.

Every once in a while on the device (I have yet to have this happen on the simulator) I don't get anymore touch events after the first one. I have attempted to debug this on the device and it appears that after I get the first touchesEnded event come in, the device is bombarded by touchesEnded calls. I NEVER get another touchesBegan call when this happens. If I press home and come back into the game, everything will usually work fine.

Here is the code for my input, as it exists in my EAGLView.m code

#pragma mark    
#pragma mark UserInputStuff
#pragma mark    

#pragma mark 
#pragma mark touchesBegan
#pragma mark    

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
firstTouch = [touch locationInView:self];
lastTouch = [touch locationInView:self];
[(MyGameAppDelegate*)[[UIApplication sharedApplication] delegate]   HandleTouchEvent:firstTouch];   

}

#pragma mark    
#pragma mark touchesEnded
#pragma mark    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event 
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self];
[(MyGameAppDelegate*)[[UIApplication sharedApplication] delegate] HandleTouchEnded:lastTouch];  
}

And here is the code as it exist in my app delegate

#pragma mark 
#pragma mark HandleTouchEnded
#pragma mark A function to react to the touch that is no longer present on the screen
#pragma mark

- (void)HandleTouchEnded:(CGPoint)coordinate
{
if(_state == kState_Title)
{
    [self TitleCollisionDetection:coordinate];
    if(_buttonHighlighted)
    {
        _textures[kTexture_Background] = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"T4Background.png"]];
        glBindTexture(GL_TEXTURE_2D, [_textures[kTexture_Background] name]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);          
        [self resetGame];
    }
}
}

And here is the code that configures the timer that fires to handle the renderer.

//Start rendering timer
_timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / kRenderingFPS) target:self   selector:@selector(renderScene) userInfo:nil repeats:YES];
[UIApplication sharedApplication].idleTimerDisabled = YES;

I'm obviously doing something dumb. What am I missing? Why is touchesEnded firing so often?


Solution

  • Actually it turned out that there was an errant second call to start the NSTimer

    _timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / kRenderingFPS) target:self selector:@selector(renderScene) userInfo:nil repeats:YES];

    This caused the program to be starved for mainline execution time as it was constantly servicing the timer routines.

    The PERFORMANCE ANALYZER IS YOUR FRIEND!!! I found this because while I thought my app was supposed to be running at 30fps, I was seeing results north of 50fps. At first I thought the performance analyzer was broken. As it turns out, it was my code.