代码之家  ›  专栏  ›  技术社区  ›  Moshe

从TouchsBegan获取Touch位置?(和其他游戏问题)

  •  7
  • Moshe  · 技术社区  · 14 年前

    我正在尝试从屏幕上获取触摸事件的位置

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

    我可以用什么代码来读取触摸坐标?到目前为止,我已经做到了:

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

    我正在尝试使用OpenGL模板应用程序制作游戏。触摸只会在 EAGLView 文件,而不是 ESRenderer1 文件。我假设我应该使用eaglview来设置变量,然后将变量从那里读取到esrenderer来执行游戏计算。这是个好方法吗?

    1 回复  |  直到 12 年前
        1
  •  16
  •   Jim Buck    14 年前

    代码如下:

    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'
    }