2009-04-21 48 views
2

我有一個簡單的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經常發射?

回答

3

實際上判明有一個錯誤的第二呼叫開始的NSTimer

_timer = [的NSTimer scheduledTimerWithTimeInterval:(1.0/kRenderingFPS)靶:自選擇器:@selector(renderScene)USERINFO:無重複:YES ]。

這會導致程序因主線執行時間不斷而服務定時器例程。

性能分析儀是您的好朋友!我發現這是因爲雖然我認爲我的應用程序應該以30fps運行,但我看到的結果是50fps以北。起初我認爲性能分析儀壞了。事實證明,這是我的代碼。

0

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

是完全相同作爲

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

你沒有受到TouchsEnded的轟炸。你的firstTouch和你的lastTouch對象是同一個對象,這讓你受到了轟炸!

你應該在你的NSLog函數touchesBegan:touchesEnded:下次確認行爲。

+0

我想你錯過了這裏的東西。 第一次觸摸電話是 [(MyGameAppDelegate *)[[UIApplication sharedApplication] delegate] HandleTouchEvent:firstTouch]; 第二個是 [(MyGameAppDelegate *)[[UIApplication sharedApplication] delegate] HandleTouchEnded:lastTouch]; – K2Digital 2009-04-21 18:39:23

相關問題