2009-04-21 47 views
2

我有一个简单的OpenGL:ES应用程序运行(它是一个游戏)。游戏加载并向用户呈现“新游戏按钮”,然后你在游戏中。我使用touchesBegan/touchesEnded来处理触摸。然后我采取坐标并相应地处理它们。touchesEnded

我也有一个运行在30Hz的NSTimer,调用renderScene来绘制屏幕上的图形。我在设备上每过一段时间(我都没有在模拟器上发生过这种情况),在第一次之后我再也没有碰到过触摸事件。我试图在设备上进行调试,看起来在第一个touchesEnded事件进入后,设备被touchesEnded调用轰击。当发生这种情况时,我永远不会再接触到touchesBegan。如果我按回家并回到游戏中,一切通常都会正常工作。

这里是我输入的代码,因为它存在于我的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; 

我明显在做一些愚蠢的事情。我错过了什么?为什么touchesEnded经常发射?

回答

3

实际上判明有一个错误的第二呼叫开始的NSTimer

_timer = [的NSTimer scheduledTimerWithTimeInterval:(1.0/kRenderingFPS)靶:自选择器:@selector(renderScene)USERINFO:无重复:YES ]。

这会导致程序因主线执行时间不断而服务定时器例程。

性能分析仪是您的好朋友!我发现这是因为虽然我认为我的应用程序应该以30fps运行,但我看到的结果是50fps以北。起初我认为性能分析仪坏了。事实证明,这是我的代码。

0

[(MyGameAppDelegate *)[[UIApplication sharedApplication] delegate] HandleTouchEnded:firstTouch];

是完全相同作为

[(MyGameAppDelegate *)[[UIApplication的sharedApplication]委托] HandleTouchEnded:lastTouch];

你没有受到TouchsEnded的轰炸。你的firstTouch和你的lastTouch对象是同一个对象,这让你受到了轰炸!

你应该在你的NSLog函数touchesBegan:touchesEnded:下次确认行为。

+0

我想你错过了这里的东西。 第一次触摸电话是 [(MyGameAppDelegate *)[[UIApplication sharedApplication] delegate] HandleTouchEvent:firstTouch]; 第二个是 [(MyGameAppDelegate *)[[UIApplication sharedApplication] delegate] HandleTouchEnded:lastTouch]; – K2Digital 2009-04-21 18:39:23

相关问题