touchesEnded

时间:2009-04-21 16:47:53

标签: iphone

我有一个简单的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];  
}

以下是我的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];
    }
}
}

以下是配置触发渲染器的计时器的代码。

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

我显然做了一些愚蠢的事。我错过了什么?为什么touchesEnded射击如此频繁?

2 个答案:

答案 0 :(得分:3)

事实上,事实证明第二次调用错误是为了启动NSTimer

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

这导致程序因主线执行时间而缺乏,因为它一直在为计时器例程提供服务。

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

答案 1 :(得分:0)

  

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

完全相同
  

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

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

下次你应该在你touchesBegan:touchesEnded:放置NSLog功能来确认行为。