Change the Sprite anchor point without moving it?

I am trying to change my sprite anchor point so that I can rotate over 0.0f, 0.0f anchorpoint. First, my rotation object at the default snap point (0.5f, 0.5f). However, later I need it to rotate over 0.0.0.0 AnchorPoint.

The problem is that I cannot change the anchor point and accordingly change the position, so it remains in the same position without quickly moving the object and moving it to the starting point.

Is there a way to set the anchor point and position of my sprite immediately, without any changes? Thanks.

-Oscar

+5
source share
4 answers

, .

, , , anchorPoint , .

:

anchorPoint . .

:

, , . ( , .)

: , 100% .

+2

UIView cocos2d:

- (void)setAnchorPoint:(CGPoint)anchorPoint forSprite:(CCSprite *)sprite
{
    CGPoint newPoint = CGPointMake(sprite.contentSize.width * anchorPoint.x, sprite.contentSize.height * anchorPoint.y);
    CGPoint oldPoint = CGPointMake(sprite.contentSize.width * sprite.anchorPoint.x, sprite.contentSize.height * sprite.anchorPoint.y);

    newPoint = CGPointApplyAffineTransform(newPoint, [sprite nodeToWorldTransform]);
    oldPoint = CGPointApplyAffineTransform(oldPoint, [sprite nodeToWorldTransform]);

    CGPoint position = sprite.position;

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    sprite.position = position;
    sprite.anchorPoint = anchorPoint;
}
+3

CCNode, abit , , . :)

1.x, 2.x. HD.

Just add this to your project and import it when you need it - it will be added to all classes originating from CCNode. (CCSprite, CCLayer)

Interface

#import "cocos2d.h"

@interface CCNode (Extensions)

// Returns the parent coordinate for an anchorpoint. Useful for aligning nodes with different anchorpoints for instance
-(CGPoint)positionOfAnchorPoint:(CGPoint)anchor;

// As above but using anchorpoint in points rather than percentage
-(CGPoint)positionOfAnchorPointInPoints:(CGPoint)anchor;

//Sets the anchorpoint, to not move the node set lockPosition to `YES`. Setting it to `NO` is equal to setAnchorPoint, I thought this would be good for readability so you always know what you do when you move the anchorpoint
-(void)setAnchorPoint:(CGPoint)a lockPosition:(BOOL)lockPosition;

@end

Implementation

#import "CCNode+AnchorPos.h"

@implementation CCNode (Extensions)

-(CGPoint)positionOfAnchorPoint:(CGPoint)anchor
{
    float x = anchor.x * self.contentSizeInPixels.width;
    float y = anchor.y * self.contentSizeInPixels.height;

    CGPoint pos = ccp(x,y);

    pos = CGPointApplyAffineTransform(pos, [self nodeToParentTransform]);

    return ccpMult(pos, 1/CC_CONTENT_SCALE_FACTOR());
}

-(CGPoint)positionOfAnchorPointInPoints:(CGPoint)anchor;
{
    CGPoint anchorPointInPercent = ccp(anchor.x/self.contentSize.width, anchor.y/self.contentSize.height);
    return [self positionOfAnchorPoint:anchorPointInPercent];
}

-(void)setAnchorPoint:(CGPoint)a lockPosition:(BOOL)lockPosition
{
    CGPoint tempPos = [self positionOfAnchorPoint:a];
    self.anchorPoint = a;

    if(lockPosition)
    {
        self.position = tempPos;
    }
}

@end
+2
source

Cocos2d-x + Fixed Scale

YourClass.h

virtual cocos2d::Vec2 positionFromSprite(cocos2d::Vec2 newAnchorPoint, cocos2d::Sprite *sprite);

YourClass.m

Vec2 YourClass::positionFromSprite(Vec2 newAnchorPoint, cocos2d::Sprite *sprite) {
    Rect rect = sprite->getSpriteFrame()->getRect();
    Vec2 oldAnchorPoint = sprite->getAnchorPoint();
    float scaleX = sprite->getScaleX();
    float scaleY = sprite->getScaleY();

    Vec2 newPoint = Vec2(rect.size.width * newAnchorPoint.x * scaleX, rect.size.height * newAnchorPoint.y * scaleY);
    Vec2 oldPoint = Vec2(rect.size.width * oldAnchorPoint.x * scaleX, rect.size.height * oldAnchorPoint.y * scaleY);

    Vec2 position = sprite->getPosition();

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    return position;
}
0
source

All Articles