0

我使用这种方法初始化我的精灵:

-(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 级。

当我记录规模时,它会以应有的规模记录,但实际图像不是。

我敢肯定,我忽略了一些简单易行的事情,但我不知道是什么。

4

1 回答 1

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);
}
于 2013-05-26T19:24:05.670 回答