0
votes

i init my sprite using this method:

-(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;
}

Everything regarding mechanics, collisions works well. The only problem is that the sprite scale doesn't change according to energy. Don't get me wrong, it's beeing added on the screen as it should, but only at scale 1.

When i log the scale, it logs at the scale it should be but the actual image is not.

I'm sure there's something simple and easy that i overlooked but i cannot figure out what.

1
do you change the scale elsewhere perhaps? Override setScale: and set a breakpoint. - LearnCocos2D
This is the only point where i set the scale.Everything else is handled by box2d. In the scene i just init the object and add it to the world. That's it. What i tried is to change the transform in nodeToParentTransform ( i know it's not indicated). It works but it doesn't scale around the set anchor point , it scales around something else. Any ideas about that? - skytz

1 Answers

0
votes

I take it you're using CCPhysicsSprite? Then check the nodeToParentTransform code, see if the x/y coords are multiplied with scaleX/Y. I believe earlier versions didn't use the scale, at least the comment seems to indicate this. For reference I'll post the method from v2.1 rc1 so you can compare and update your version's transform code:

-(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);
}