iPhone或cocos2d在处理视网膜显示触摸时出现错误?
我试图在我的cocos2d 1.01rc iPhone应用程序中处理视网膜显示触摸,发现要做到这一点,我必须将触摸点乘以比例因子(见下面的代码),即使我在AppDelegate中设置为视网膜显示(见页面末尾的代码)。我有点困惑,因为我本以为locationInView功能会检索视网膜显示触摸,而不是“标准”480x640分辨率触摸。我的猜测是,这是因为locationInView来自ios库,而不是cocos2d,并且cocos2d应用程序委托中的视网膜显示设置在ios级别之前不会传播。奇怪。我把下面的代码和输出放在清晰的地方,但是如果你有类似的问题,如果我应该考虑这个“Bug”,就可以在COCOS2D和iOS SDK之间丢失其他东西作为警告铃铛。我可能只是太傻了,没有找到合适的文档页面 这是我在ccTouchesEnded事件上运行的代码:iPhone或cocos2d在处理视网膜显示触摸时出现错误?,iphone,cocoa-touch,cocos2d-iphone,Iphone,Cocoa Touch,Cocos2d Iphone,我试图在我的cocos2d 1.01rc iPhone应用程序中处理视网膜显示触摸,发现要做到这一点,我必须将触摸点乘以比例因子(见下面的代码),即使我在AppDelegate中设置为视网膜显示(见页面末尾的代码)。我有点困惑,因为我本以为locationInView功能会检索视网膜显示触摸,而不是“标准”480x640分辨率触摸。我的猜测是,这是因为locationInView来自ios库,而不是cocos2d,并且cocos2d应用程序委托中的视网膜显示设置在ios级别之前不会传播。奇怪。我
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
for(int i=0; i<[[touches allObjects] count]; i++){
UITouch *touch = [[touches allObjects] objectAtIndex:i];
CGPoint point = [touch locationInView: [touch view]];
CCLOG(@"Before x:%f y:%f", point.x, point.y);
point = [[CCDirector sharedDirector] convertToGL: point];
float factor = CC_CONTENT_SCALE_FACTOR();
CCLOG(@"factor %f:", factor);
CCLOG(@"After x:%f y:%f", point.x , point.y );
CCLOG(@"After x:%f y:%f", point.x * factor, point.y * factor);
...
}
该应用程序在第四代iPod touch的视网膜显示屏下运行,以下是一些初始化数据:
cocos2d: cocos2d v1.0.1
cocos2d: GL_VENDOR: Imagination Technologies
cocos2d: GL_RENDERER: PowerVR SGX 535
cocos2d: GL_VERSION: OpenGL ES-CM 1.1 IMGSGX535-63.14.2
cocos2d: GL_MAX_TEXTURE_SIZE: 2048
cocos2d: GL_MAX_MODELVIEW_STACK_DEPTH: 16
cocos2d: GL_MAX_SAMPLES: 4
cocos2d: GL supports PVRTC: YES
cocos2d: GL supports BGRA8888 textures: YES
cocos2d: GL supports NPOT textures: YES
cocos2d: OS version: 5.0.1 (0x05000100)
cocos2d: surface size: 640x960
我查看了google和stackoverflow,在我看来这是一个一年前就知道的bug,我只是不能100%确定这是否已在rc1.01版本中修复,或者我是否在我的AppDelegate中做了一些愚蠢的事情:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
//added
CC_DIRECTOR_INIT();
// Init the window
// window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
// Init the View Controller
// viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
// viewController.wantsFullScreenLayout = YES;
//
// Create the EAGLView manually
// 1. Create a RGB565 format. Alternative: RGBA8
// 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
//
//
EAGLView *glView = [director openGLView];
[glView setMultipleTouchEnabled:YES];
// attach the openglView to the director
[director setOpenGLView:glView];
// // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
-(void)applicationdFinishLaunching:(UIApplication*)应用程序
{
//增加
CC_DIRECTOR_INIT();
//打开窗户
//窗口=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]边界]];
//尝试使用CADisplayLink控制器
//如果失败(SDK<3.1),则使用默认控制器
if(![CCDirector setDirectorType:kCCDirectorTypeDisplayLink])
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector*董事=[CCDirector sharedDirector];
[控制器设置设备定向:KCCD设备定向肖像];
//初始化视图控制器
//viewController=[[RootViewController alloc]initWithNibName:nil bundle:nil];
//viewController.WantFullScreenLayout=是;
//
//手动创建EAGLView
//1.创建RGB565格式。备选方案:RGBA8
//2.0位深度格式。使用16或24位进行3d效果,如CCPageTurnTransition
//
//
EAGLView*glView=[director openGLView];
[glView SETMULTIPLETOUCHEBLED:是];
//将openglView附加到控制器
[director setOpenGLView:glView];
////在iPhone 4上启用高分辨率模式(视网膜显示),并在所有其他设备上保持低分辨率
如果(![director enableRetinaDisplay:是])
CCLOG(@“视网膜显示不受支持”);
有什么想法吗?这会影响我的代码的其他方面吗?它似乎只涉及触摸检测,对于这一点,乘以比例因子的修正似乎是正确的,当我访问精灵对象的相对位置时,我似乎没有问题。我将对此做更多的测试,并记住这一点
谢谢!在iOS(和Cocos)中,触摸和很多东西(如视图位置/大小)不是以像素为单位处理的,而是以点为单位
在非视网膜屏幕中,一个点正好是一个像素,但在视网膜显示器中,一个点是两个像素宽两个像素高
这意味着您的所有触摸和其他坐标将始终在标准显示器的范围内(320像素宽,480像素高)。这非常好,因为您不必担心屏幕的类型,而且您的代码更干净
iOS和Cocos几乎都在幕后管理高分辨率,因此加载的图像将是高分辨率的,但您不必担心代码中有更多像素的屏幕
(如果出于某种特定原因,你想使用像素而不是点,那么你所做的是正确的,用点乘以内容大小。苹果用点做的背后的想法是,你不必为此烦恼)试试看
CGPoint point = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:point];
CGPoint locationInSelf = [self convertToNodeSpace:convertedLocation];
我认为cocos在convertToNodeSpace中应用了适当的比例因子。依赖API将使您的代码在未来的任何潜在变化中(无论是在cocos中还是在apple的东西中)都能生存下来
至于对视网膜的担忧,我的总工作时间几乎为零(我只需要处理一些我变得有创意并偏离了上述非高清版本中关于“生存”的陈述的情况)
这里不需要使用因子进行乘法。
因为在视网膜显示器中,640*960是像素大小,对于触摸处理,它们返回点,所以去除因子乘法。这可能对您有所帮助。这是设计的
您必须了解,iOS设备以点为单位报告触摸位置。视网膜和非视网膜设备的点分辨率均为480x320点。在视网膜设备上,一个点由4个像素组成,而在所有其他设备上,一个点等于屏幕上的一个像素
根据视网膜上的触摸,设备可以报告为.5个位置(阅读第一个注释框),为您提供完整的视网膜触摸分辨率。如果您没有,则可能在转换过程中丢失该分辨率(检查原始的UITouch位置).无论如何,触摸的视网膜分辨率太精确了,无法使用,480x320足够触摸了。嗨,Steffen,谢谢。我明白了。这有点让人困惑,因为我将设备设置为纵向,左下角的触摸现在是y:480和x:0,rop右下角是y:0和x:320。我期待着左下角的t为y:0。我将尝试了解原因。感谢您的回答:)
CGPoint point = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:point];
CGPoint locationInSelf = [self convertToNodeSpace:convertedLocation];
CCLOG(@"After x:%f y:%f", point.x * factor, point.y * factor);