taoru's memo

Objective-c,cocos2d,など開発についてのメモ(andoroidも少しだけ)

CCSpriteのanchorPointを変更した際の位置ズレを吸収する

CCSpriteはanchorPointをセットすると位置がズレる。
瞬間的に位置がズレてしまうので、ズレないように見せるためにpositionを補正する

CCSprite *sprite = [CCSprite spriteWithFile:@"hoge.png"];
sprite.anchorPoint = ccp(0.5f, 0.5f);

// 位置ズレ前のboundingBox
CGRect oldBoundingBox = sprite.boundingBox;

sprite.anchorPoint = ccp(1.0f, 0.5f);

// 位置ズレ後のboundingBox
CGRect newBoundingBox = sprite.boundingBox;

// anchorPoint変更で発生した差分
CGPoint diff = ccpSub(newBoundingBox.origin, oldBoundingBox.origin);

// 位置ズレ差分を引くことで見た目上同じ位置になるようにセット
sprite.position = ccpSub(sprite.position, diff);
QLOOKアクセス解析