我有一個簡單的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