我使用这种方法初始化我的精灵:
-(id)initAttackerWithType:(ShapeType)type withPosition:(CGPoint)pos world:(b2World*)world andEnergy:(float)energy {
if(([self initWithTexture:[[CCTextureCache sharedTextureCache]addImage:@"circle.png"]])){
self.tag=type;
self.color= type == Enemy ? ccc3(255, 0, 0) : ccc3(0, 255, 0);
self.scale=energy *.22/PTM_RATIO; //scale depending on energy
// 0.22 gets the sprite scale to synchronize with shape radius
self.position=pos;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(pos.x/PTM_RATIO, pos.y/PTM_RATIO);
_body = world->CreateBody(&bodyDef);
b2CircleShape dynamicCircle;
dynamicCircle.m_radius=energy/PTM_RATIO; //radius depending on energy
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicCircle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0;
fixtureDef.restitution = 1;
_body->CreateFixture(&fixtureDef);
fixtureDef.userData=self;
_body->SetUserData(self);
}else self = nil;
return self ? self : nil;
}
关于力学,碰撞的一切都很有效。唯一的问题是精灵比例不会根据能量而变化。 不要误解我的意思,它应该在屏幕上添加,但只能在规模1上。
当我记录刻度时,它会以应有的刻度记录,但实际图像不是。
我确信我忽略了一些简单易行的东西,但我无法弄清楚是什么。
答案 0 :(得分:0)
我认为你使用的是CCPhysicsSprite?然后检查nodeToParentTransform代码,查看x / y坐标是否与scaleX / Y相乘。我相信早期版本没有使用规模,至少评论似乎表明了这一点。作为参考,我将从v2.1 rc1发布方法,以便您可以比较和更新版本的转换代码:
-(CGAffineTransform) nodeToParentTransform
{
// Although scale is not used by physics engines, it is calculated in case
// the sprite is animated (scaled up/down) using actions.
// For more info see: http://www.cocos2d-iphone.org/forum/topic/68990
cpVect rot = (_ignoreBodyRotation ?
cpvforangle(-CC_DEGREES_TO_RADIANS(_rotationX)) :
_cpBody->rot);
CGFloat x = _cpBody->p.x + rot.x * -_anchorPointInPoints.x * _scaleX -
rot.y * -_anchorPointInPoints.y * _scaleY;
CGFloat y = _cpBody->p.y + rot.y * -_anchorPointInPoints.x * _scaleX +
rot.x * -_anchorPointInPoints.y * _scaleY;
if (_ignoreAnchorPointForPosition)
{
x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y;
}
return _transform = CGAffineTransformMake(rot.x * _scaleX, rot.y * _scaleX,
-rot.y * _scaleY, rot.x * _scaleY,
x, y);
}