我运行了一个简单的OpenGL:ES应用程序(这是一个游戏)。游戏加载并向用户显示“新游戏按钮”,然后您就进入了游戏。我正在使用touchesBegan / touchesEnded处理触摸。然后,我获取坐标并进行相应处理。
我还有一个以30Hz运行的NSTimer,它调用renderScene绘制屏幕上的图形。
每隔一段时间在设备上(我尚未在模拟器上发生这种情况),在第一个事件之后,我不再有触摸事件。我试图在设备上调试此设备,看来当我收到第一个touchesEnded事件后,设备就被touchesEnded调用轰炸了。发生这种情况时,我再也不会碰到电话。如果我按下主屏幕并重新进入游戏,则通常一切正常。
这里是我输入的代码,因为它存在于我的EAGLView.m代码中
#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];
}
这是我的应用程序委托中存在的代码
#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];
}
}
}
这是配置触发计时器以处理渲染器的代码。
//Start rendering timer
_timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / kRenderingFPS) target:self selector:@selector(renderScene) userInfo:nil repeats:YES];
[UIApplication sharedApplication].idleTimerDisabled = YES;
我显然在做些愚蠢的事情。我想念什么?为什么touchedEnd触发如此频繁?
实际上结果是有第二次启动NSTimer的错误呼叫
_ timer = [NSTimer ScheduledTimerWithTimeInterval:(1.0 / kRenderingFPS)目标:自我选择器:@selector(renderScene)userInfo:无重复:是];
这会导致程序在为主程序执行时间感到饥饿,因为它一直在为计时器例程提供服务。
性能分析仪是您的朋友!!!我发现这个问题是因为我以为我的应用程序应该以30fps的速度运行,但是却看到50fps以上的结果。起初我以为性能分析器坏了。事实证明,这是我的代码。
[((MyGameAppDelegate *)[[UIApplication sharedApplication]委托] HandleTouchEnded:firstTouch];
与]完全相同>
[((MyGameAppDelegate *)[[UIApplication sharedApplication]委托] HandleTouchEnded:lastTouch];
您不会被TouchsEnded轰炸。您的firstTouch和lastTouch对象是同一对象的事实使您大吃一惊!
您下次应在touchesBegan:
和touchesEnded:
中放置NSLog函数以确认行为。