How to center the alignment of a sprite?

In cocos2d, does anyone know how to center a sprite? Right now I have a sprite that moves to where you touch the screen. The problem is that the sprite is aligned to the bottom left corner. This means that instead of moving down, if you touch just below the bottom, the sprite will move up. Thanks in advance!

Here is my code ...

(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];

    [mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp(point.x, 480 - point.y)]];

    return YES;
}

      

+1


a source to share


2 answers


The core of the default transformation for sprites in Cocos2D is the center of the sprite, so it should move so that the center of the sprite falls into the affected location, as you do now. Have you changed the sprite transformation anchor?

The only thing I can think of is that if your mainSprite is a child of another CocosNode, you may need to convert touch coordinates to node space using this method:

- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;

      



... on the parent node. However, I doubt this is the problem. Sorry if this is useless.

EDIT: OP, if you read this, which version of Cocos2D are you using? I believe 0.8 (currently in the svn torso) changes how the binding works; it may be helpful for others to know what you are working with for future reference.

+1


a source


I did it! For those who want to know there is a code here ...

[mainSprite setTransformAnchor:ccp(24.0, 64.5)];

      



24 is half the width of the sprites

64.5 is half the height of the sprites

0


a source







All Articles