【问题标题】:Retina Display coordinate is wrong视网膜显示坐标错误
【发布时间】:2011-09-17 17:03:07
【问题描述】:

我尝试在 Retina iPhone 的屏幕上显示图像。 所以我使用了这段代码。

#define ScreenWidth 960
#define ScreenHeight 640

void ConvertCoordf(float* x, float* y)  
{  
    int height = ScreenHeight;  
    if ( height / 2 > *y )  
        *y += (height / 2 - *y) * 2;  
    else if ( height / 2 < *y )  
        *y -= (*y - height / 2) * 2;      
}

void DISPLAY_UPDATE(char *name, int x , int y , int startx, int starty , int w , int h ,float rotation)  
{  
    CCSprite *sprite = [[CCSprite spriteWithFile:[NSString stringWithUTF8String:name] rect:CGRectMake(startx, starty, w, h)] retain];  
    float drawX = x, drawY = y;   
    CGSize size = [sprite contentSize];

    int nWidth = size.width;
    int nHeight = size.height;
    drawX = drawX + nWidth/2;
    drawY = drawY - nHeight/2;

    ConvertCoordf(&drawX, &drawY);
    drawY -= nHeight;
    [sprite setPosition:ccp(drawX, drawY)];
    [sprite setAnchorPoint:CGPointMake(0.5, 0.5)];
    [sprite setRotation:rotation];
    [_mainLayer addChild:sprite];
    [sprite release];
}

很遗憾,图片无法正确显示。 图片位置不对。 但在其他iphone中,它显示正确。 怎么了? 这个坐标转换有什么问题?

截图

【问题讨论】:

  • 一些截图会很好。

标签: objective-c xcode4 cocos2d-iphone iphone-4


【解决方案1】:

这是完全错误的:

#define ScreenWidth 960
#define ScreenHeight 640

这些宏的正确值:

#define ScreenWidth 480
#define ScreenHeight 320

这是因为您没有使用像素。您正在处理点,系统将为您计算像素。这意味着 iPhone 屏幕始终为 480x320 points。您也可以使用像素,但您必须查看文档。
很快我会在这里添加一个链接,指向文档中这个问题的描述。


编辑添加的文档链接:Points Versus Pixels

【讨论】:

  • 然后,将大小更改为 480 X 320 后,Retina 显示的背景图像大小为 960 X 480。因此坐标转换结果为 drawX = -240,drawY = -160。而且它不能正确绘制。
  • 那你得检查你的计算了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多