Cocos2d 2.0 - 将精灵集中在一个图层上

Spa*_*Dog 0 objective-c cocos2d-iphone ios

我有一个CClayer类,当这个类进入它时会创建一个应该居中的CCSprite,所以稍后,当我旋转用该CCLayer类创建的对象时,它会围绕它的中心旋转.我的意思是,如果该类的精灵是200像素宽和300像素高的图像,我希望CCLayer枢轴为100,150.

我试图将其设置为0,0和0.5,0.5但没有成功.

据我了解,CCLayer没有边界框,它就像一种节点,对吧?所以,我创建这样的类:

-(id) initWithImage:(UIImage*)image Name:(NSString*)name
{

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

        self.isTouchEnabled = YES;

        self.mySprite = 
            [CCSprite spriteWithCGImage:image.CGImage key:name];

        self.mySprite.position = CGPointZero;
        [self addChild:self.mySprite];

        self.mySprite.anchorPoint = ccp(0.0f, 0.0f);
        // have tried also 0.5f, 0.5f... no success        
    }

    return self;
}
Run Code Online (Sandbox Code Playgroud)

我怎么做?

谢谢

Jam*_*ter 6

在CCLayer子类中提供一个方法来旋转sprite:

-(void) rotateMySpriteToAngle:(float) angle
{
    self.mySprite.rotation = angle;
}
Run Code Online (Sandbox Code Playgroud)

精灵的锚点应该(0.5, 0.5)围绕它的中心旋转.

我觉得你的节目太复杂了吗?你可以使用精灵而不是一个带有精灵的图层作为孩子吗?然后你可以直接旋转它.

它看起来好像你想让你的精灵可触摸.如果您正在寻找实现按钮,请考虑使用CCMenuCCMenuItem.


编辑

尝试将图层的锚点设置为(0, 0)和精灵的锚点(0.5, 0.5),然后将精灵的位置设置为(0, 0)

这意味着精灵的中心(0, 0)位于图层上,然后围绕它的原点旋转图层.

                    Scene
=============================================
=                                           =
=                                           =
=                                           =
=                                           =
=       |                                   =
=       | Layer (effective infinite size)   =
=     __|__                                 =
=    |  |  |                                =
=    |  +--|--------------                  =
=    |_____|                                =
=     Sprite                                =
=============================================
The + is the origin of the layer and the center point of the sprite
When you rotate the layer around it's origin, you are simultaneously rotating the sprite about it's centre.