【问题标题】:The device is bombarded by touchesEnded calls设备被 touchesEnded 呼叫轰炸
【发布时间】:2010-10-20 21:19:14
【问题描述】:

我有一个简单的 OpenGL:ES 应用程序正在运行(它是一个游戏)。游戏加载并向用户显示“新游戏按钮”,然后您就进入了游戏。我正在使用 touchesBegan / touchesEnded 来处理触摸。然后我获取坐标并相应地处理它们。

我还有一个运行频率为 30Hz 的 NSTimer,它调用 renderScene 来绘制屏幕上的图形。

在设备上每隔一段时间(我还没有在模拟器上发生这种情况)我在第一个之后不再收到触摸事件。我试图在设备上调试它,似乎在我收到第一个 touchesEnded 事件后,设备被 touchesEnded 调用轰炸了。发生这种情况时,我再也没有接到过 touchesBegan 电话。如果我按下 home 键并回到游戏中,通常一切都会正常工作。

这是我的输入代码,因为它存在于我的 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 如此频繁地触发?

【问题讨论】:

    标签: ios iphone cocoa-touch


    【解决方案1】:

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

    完全一样

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

    您不会被 TouchsEnded 轰炸。你被你的 firstTouch 和你的 lastTouch 对象是同一个对象这一事实轰炸了!

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

    【讨论】:

    • 我想你在这里遗漏了一些东西。 firstTouch 调用是 [(MyGameAppDelegate*)[[UIApplication sharedApplication] delegate] HandleTouchEvent:firstTouch];第二个是 [(MyGameAppDelegate*)[[UIApplication sharedApplication] delegate] HandleTouchEnded:lastTouch];
    【解决方案2】:

    实际上,有一个错误的第二次调用来启动 NSTimer

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

    这导致程序在主线执行时间上处于饥饿状态,因为它一直在为计时器例程提供服务。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-30
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      相关资源
      最近更新 更多