【发布时间】:2012-07-10 11:30:55
【问题描述】:
我正在尝试让 cocos2d/box2d 游戏在 iPad、iPhone 和 iPhone Retina 上运行。 我的问题是,视网膜模拟器上的fixture和body不对齐,请点击下面的截图进行说明(作为stackoverflow的新成员,它不允许我在这里发布截图)。
(请忽略不同的形状,我希望4个角对齐) 在过去的几天里,我对此进行了相当多的研究,我发现最接近的是:
但是 PTM_RATIO 和 CC_CONTENT_SCALE_FACTOR() 提供的解决方案在我的情况下似乎不起作用。我认为这与我没有将文件中的图像加载到我的精灵中这一事实有关。这个问题的大多数解决方案都是基于为视网膜显示器加载 -hd 图像文件,但我根本不想在我的游戏中使用文件。我基本上想在运行时自己绘制多边形,
我的代码如下:
-(CCSprite*)addSprite
{
CGSize contextsize = CGSizeMake(200, 200);
UIGraphicsBeginImageContext(contextsize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFlush(context);
CGContextSetAllowsAntialiasing(context, true);
CGContextTranslateCTM(context, 0, contextsize.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
UIBezierPath* aPath;
aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100)
radius:100
startAngle:0
endAngle:1.57
clockwise:YES];
[aPath addArcWithCenter:CGPointMake(100, 100)
radius:50
startAngle:1.57
endAngle:0
clockwise:NO];
[aPath stroke];
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
UIImage *graphImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:graphImage] autorelease];
CCSprite *sprite = [CCSprite spriteWithTexture:tex];
return sprite;
}
-(void) addFixture:(CCSprite *)fixsprite
{
b2Vec2 arcdots[] = {
b2Vec2(50.0f / PTM_RATIO, 0.0f / PTM_RATIO),
b2Vec2(100.0f / PTM_RATIO, 0.0f / PTM_RATIO),
b2Vec2(0.0f / PTM_RATIO, 100.0f / PTM_RATIO),
b2Vec2(0.0f / PTM_RATIO, 50.0f / PTM_RATIO)
};
b2PolygonShape p_shape;
b2FixtureDef fixtureDef;
b2BodyDef bodyDef;
bodyDef.type = b2_kinematicBody;
bodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
bodyDef.userData = fixsprite;
b2Body *body = world->CreateBody(&bodyDef);
p_shape.Set(arcdots, 4);
fixtureDef.shape = &p_shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}
我从主程序中调用这些函数如下:
CCSprite *sprite2 = [self addSprite];
sprite2.position = ccp(0, 0);
[self addChild:sprite2 z:0];
[self addFixture:sprite2];
我在委托文件中没有注释这些行:
if( ! [director enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
如果需要更多信息,请告诉我。请温柔一点,我才刚刚开始学习这个。感谢您的宝贵时间。
【问题讨论】:
标签: iphone objective-c ipad cocos2d-iphone box2d