Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am doing some tests with animations in Cocos2D using sprite-sheets. The animation works ok except that the anchor-point is in the center. I need to put it in the bottom. With the code below i am not able to add an anchor-point to my sprites, after quite a few tests.

Would someone nice be able to help me with this?

-(id) init {

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

        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ratHammer.plist" textureFile:@"ratHammer.pvr.ccz"];
        CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"ratHammer.pvr.ccz"];
        [self addChild:spriteSheet];
        NSMutableArray *walkAnimFrames = [NSMutableArray array];
        for (int i = 0; i <= 5 ; ++i) {
            [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"ratHammer_anim%d.png", i]]];
    }

    CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];

    CGSize winSize = [CCDirector sharedDirector].winSize;
    self.bear = [CCSprite spriteWithSpriteFrameName:@"ratHammer_anim0.png"];        
    _bear.position = ccp(winSize.width/2, winSize.height/2);
    self.walkAction = [CCRepeatForever actionWithAction:
                       [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    [_bear runAction:_walkAction];
    [spriteSheet addChild:_bear];


}
return self;

}
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You can modify the anchor point location by setting the anchorPoint property of your sprite.

The anchor-point should be given as CGPoint and is normalized to 0..1 for both axes. Per default it's at 0.5, 0.5. The lower left would be 0.0, 0.0.

To set the anchor point to the lower left, you would use something like this:

[spriteSheet setAnchorPoint: ccp(0.0f, 0.0f)];
[_bear setAnchorPoint: ccp(0.0f, 0.0f)];

If you need further information, here are the relevant API docs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.