我需要绘制一个行为box2d的行,并将由CCSprite对象绘制。 现在我可以绘制非常好的线(仅在调试绘制时可见)但我不知道,从绘制时我可以获得所有点的位置。 如果代码如下所示,CCSprite图像仅在用户绘制非常慢时出现。 当用户快速绘制时,我们只能看到很少的图像点。 我想我应该从SetAsEdge函数获得积分,并且每一个绘制CCSprite,但我不知道如何得到这一点。
CGPoint start = [touch locationInView: [touch view]];
start = [[CCDirector sharedDirector] convertToGL: start];
CGPoint end = [touch previousLocationInView:[touch view]];
end = [[CCDirector sharedDirector] convertToGL:end];
float distance = ccpDistance(start, end);
if (distance > 1)
{
int d = (int)distance;
b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
CCSprite *obj = [CCSprite spriteWithFile:@"image.png"];
b2BodyDef bd;
bd.userData = obj;
bd.type = b2_staticBody;
bd.position.Set(0, 0);
obj.position = ccp(start.x,start.y);
[self addChild:obj z:1];
b2Body* body = _world->CreateBody(&bd);
b2PolygonShape shape;
shape.SetAsEdge(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
body->CreateFixture(&shape, 0.0f);
}
答案 0 :(得分:0)
好的,我找到了解决方案,也许这对其他人有用。
CGPoint start = [touch locationInView: [touch view]];
start = [[CCDirector sharedDirector] convertToGL: start];
CGPoint end = [touch previousLocationInView:[touch view]];
end = [[CCDirector sharedDirector] convertToGL:end];
float distance = ccpDistance(start, end);
if (distance > 1)
{
int d = (int)distance;
b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
b2BodyDef bd;
bd.type = b2_staticBody;
bd.position.Set(0, 0);
b2Body* body = _world->CreateBody(&bd);
b2PolygonShape shape;
shape.SetAsEdge(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
body->CreateFixture(&shape, 0.0f);
CGPoint diff = ccpSub(start, end);
float rads = atan2f( diff.y, diff.x);
float degs = -CC_RADIANS_TO_DEGREES(rads);
float dist = ccpDistance(end, start);
CCSprite *obj = [CCSprite spriteWithFile:@"image.png"];
[obj setAnchorPoint:ccp(0.0f, 0.5f)];
[obj setPosition:end];
[obj setScaleX:dist/obj.boundingBox.size.width];
[obj setRotation: degs];
[self addChild:obj];
}