2010-07-04 93 views
7

我試圖從屏幕上獲取觸摸事件的位置從touchesBegan獲取觸摸位置? (和其他遊戲問題)

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

我可以使用什麼代碼讀取觸摸座標?我已經得到了這麼遠:

NSLog(@"Touch data: %@", [NSString stringWithFormat:@"NSSet: %@",[touches description]]); 

我想使用OpenGL模板應用程序進行遊戲。觸摸只會在EAGLView文件中註冊,而不會在ESRenderer1文件中註冊。我假設我應該使用EAGlView來設置變量,然後從那裏讀取變量到ESRenderer來執行遊戲計算。這是一個好方法嗎?

回答

15

下面的代碼做的:

NSArray *touchesArray = [touches allObjects]; 
for(int i=0; i<[touchesArray count]; i++) 
{ 
    UITouch *touch = (UITouch *)[touchesArray objectAtIndex:i]; 
    CGPoint point = [touch locationInView:nil]; 

    // do something with 'point' 
} 
+5

我會選擇使用'爲(UITouch *在觸摸觸摸){/*...*/}'代替。 (快速枚舉!) – bddckr 2010-07-04 21:45:52

+0

如果我只是想要最新的觸摸,沒有其他信息呢?這將如何工作? – Moshe 2010-07-04 21:54:22

+0

我認爲該數組是「最新」觸摸的列表,它表示當前手指在屏幕上的位置。大多數情況下,陣列對於大多數應用程序只有一個元素。 – 2010-07-05 02:47:29