gpt4 book ai didi

ios - 设备受到 TouchEnded 调用的轰炸

转载 作者:行者123 更新时间:2023-12-03 21:00:29 27 4
gpt4 key购买 nike

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

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

每隔一段时间在设备上(我还没有在模拟器上发生这种情况)我在第一个触摸事件之后就不再收到触摸事件了。我尝试在设备上对此进行调试,似乎在我收到第一个 TouchEnded 事件后,设备就会受到 TouchEnded 调用的轰炸。发生这种情况时,我再也没有接到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];
}

这是我的应用程序委托(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;

我显然在做一些愚蠢的事情。我缺少什么?为什么 TouchEnded 如此频繁地触发?

最佳答案

实际上,第二次调用 NSTimer 时出现了错误

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

这导致程序缺乏主线执行时间,因为它不断地为计时器例程提供服务。

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

关于ios - 设备受到 TouchEnded 调用的轰炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/773558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com