0
votes

I have a CClayer class and when this class inits it creates a CCSprite that should be centered, so later, when I rotate an object created with that CCLayer class, it rotates around its center. I mean, if the sprite on that class is an image 200 pixels wide and 300 pixels height, I want the CCLayer pivot to be at 100,150.

I have tried to set it at 0,0 and 0.5,0.5 without success.

As far as I understand, CCLayer has no bounding box, it is like a kind of node, right? so, I create the class like this:

-(id) initWithImage:(UIImage*)image Name:(NSString*)name
{

    if( (self=[super init])) {

        self.isTouchEnabled = YES;

        self.mySprite = 
            [CCSprite spriteWithCGImage:image.CGImage key:name];

        self.mySprite.position = CGPointZero;
        [self addChild:self.mySprite];

        self.mySprite.anchorPoint = ccp(0.0f, 0.0f);
        // have tried also 0.5f, 0.5f... no success        
    }

    return self;
}

How do I do that?

thanks

1

1 Answers

6
votes

Provide a method in your CCLayer subclass to rotate the sprite:

-(void) rotateMySpriteToAngle:(float) angle
{
    self.mySprite.rotation = angle;
}

The anchor point of the sprite should be (0.5, 0.5) to rotate it about its centre.

I feel you are making your program too complex though? Could you just use a sprite instead of a layer with a sprite as a child? Then you could rotate it directly.

It looks as though you want to make your sprite touchable. Consider using CCMenu and CCMenuItems if you are looking to implement buttons.


Edit

Try setting the anchor point of the layer to (0, 0) and the anchor point of the sprite to (0.5, 0.5), then set the position of the sprite to (0, 0)

This means the centre of the sprite is at (0, 0) on the layer and you then rotate the layer around it's origin.

                    Scene
=============================================
=                                           =
=                                           =
=                                           =
=                                           =
=       |                                   =
=       | Layer (effective infinite size)   =
=     __|__                                 =
=    |  |  |                                =
=    |  +--|--------------                  =
=    |_____|                                =
=     Sprite                                =
=============================================
The + is the origin of the layer and the center point of the sprite
When you rotate the layer around it's origin, you are simultaneously rotating the sprite about it's centre.