http://blog.sina.com.cn/s/blog_7a2ffd5c0100x4q6.html
Sprite如果经过了缩放,那么构建的CCMenuItemSprite显示的位置会不正确。
一个Sprite经过缩放后,实际尺寸不再等于contentSize,而是需要乘以scale系数,但是在CCMenuItemSprite构造时,仍然设定了Sprite的contenSize大小:
[self setContentSize: [normalImage_ contentSize]];
而后设定3种状态的Sprite时,会将其锚点设为(0,0),如:
-(void) setNormalImage:(CCNode <CCRGBAProtocol>*)image
{
if( image != normalImage_ ) {
image.anchorPoint = ccp(0,0);
image.visible = YES;
[self removeChild:normalImage_ cleanup:YES];
[self addChild:image];
normalImage_ = image;
}
}
这样NormalSprite就可以完全显示在CCMenuItemSprite中了,如果Sprite经过了缩放,CCMenuItemSprite是没有缩放的,这样Sprite的显示位置就会以(0,0)为准了。比如你缩小了Sprite,则看到的效果就是图片偏左下方。
其实比较简单的方法是,不对Sprite进行缩放,而对CCMenuItemSprite进行缩放。但是如果我们实际拿到的Sprite已经经过了缩放,则要注意设定CCMenuItemSprite的尺寸时,考虑到缩放比例以进行偏移。