我有一个简单的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经常发射?
我想你错过了这里的东西。 第一次触摸电话是 [(MyGameAppDelegate *)[[UIApplication sharedApplication] delegate] HandleTouchEvent:firstTouch]; 第二个是 [(MyGameAppDelegate *)[[UIApplication sharedApplication] delegate] HandleTouchEnded:lastTouch]; – K2Digital 2009-04-21 18:39:23