升级到 cocos2d-iphone 2.0 版后,我在绘制对象的坐标时遇到了问题。据我所知,我已经正确升级了所有模板,但仍然存在问题。例如,在“Learning Cocos2D”一书中(第 10 章 box2D)中,我已经实现了几个简单的方法来首先 createGround 和 createBoxAtLocation。第一个方法是从init调用的,如下:
- (void)createGround {
CGSize winSize = [[CCDirector sharedDirector] winSize];
// Set up the Box2D coordinates that correspond to the four corners of the ground boundary
float32 margin = 10.0f;
b2Vec2 lowerLeft = b2Vec2(margin/PTM_RATIO, margin/PTM_RATIO);
b2Vec2 lowerRight = b2Vec2((winSize.width-margin)/PTM_RATIO, margin/PTM_RATIO);
b2Vec2 upperRight = b2Vec2((winSize.width-margin)/PTM_RATIO,
(winSize.height-margin)/PTM_RATIO);
b2Vec2 upperLeft = b2Vec2(margin/PTM_RATIO,
(winSize.height-margin)/PTM_RATIO);
// Define the static container body, which will provide the collisions at screen borders.
b2BodyDef screenBorderDef;
screenBorderDef.position.Set(0, 0);
b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
b2EdgeShape screenBorderShape;
// Create fixtures for the four borders (the border shape is re-used)
screenBorderShape.Set(lowerLeft, lowerRight);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(lowerRight, upperRight);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperRight, upperLeft);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperLeft, lowerLeft);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
}
这应该在屏幕边缘周围绘制一个绿色边界,边距为 10 点。但是,我只能看到底部和左侧边缘的绿色边界。上边缘和右边缘分别以屏幕高度和宽度的 2 倍绘制。
在同一个 Box2D 示例中,将在用户触摸屏幕的点绘制一个框。当用户触摸屏幕时,方法 createBoxAtLocation 会调用触摸的位置。触摸位置坐标在 cocos2d 点坐标中,并且在您期望的范围内。下面是 createBoxAtLocation 方法:
- (void)createBoxAtLocation:(CGPoint)location
withSize:(CGSize)size
{
// First create a body
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody; // This box is a dynamic body (movement handled by box2d)
bodyDef.position = b2Vec2(location.x/PTM_RATIO, // convert the point location to meters location
location.y/PTM_RATIO);
bodyDef.allowSleep = false;
b2Body *body = world->CreateBody(&bodyDef);
// Now create one or more fixtures to add to the body.
// To do this, you must first create a shape. In this case, we are creating a square box
b2PolygonShape shape;
shape.SetAsBox(size.width/2/PTM_RATIO, size.height/2/PTM_RATIO); // SetAsBox takes half dimensions as parameters
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0; // set density. The higher the number, the heavier it is
body->CreateFixture(&fixtureDef);
}
例如,如果触摸位置是点坐标 193、190.5(在 iphone 视网膜显示器上),则 createBoxAtLocation 方法将在点坐标 386、381 处绘制一个框,即使 location.x = 193 和 location.y = 190.5。请注意,bodyDef.position 的 box2d 坐标为 3.86、3.81,因为 PTM_RATIO 为 50。
我也可以说,当我运行模拟器并选择 iphone(非视网膜)作为硬件时,框会在正确的位置创建,并且 createGround 在屏幕空间内生成正确的边界。我也可以说代码可以识别所有设备的正确尺寸屏幕(例如 cocos2d 打印出 cocos2d:表面尺寸:iPhone 的 480x320 和 cocos2d:表面尺寸:iPhone 3.5 视网膜的 960x640)。最后,我运行了调试器并深入研究了绘图程序。据我所知和理解,绘图程序知道正确的 box2d 坐标。但是该框只是显示在一个 2X 的坐标上,它应该是视网膜显示器的坐标。
我怀疑 createGround 的问题和 createBoxAtLocation 的问题是相同的。我确信有一个简单的解决方案,但我无法弄清楚。有什么建议么?