Cocos2D帮助:如何使用父精灵旋转子精灵,其中子精灵只移动而不旋转?

时间:2012-02-22 08:07:12

标签: objective-c cocos2d-iphone

我的问题:我创建了一个永远旋转的父精灵。我已经为它创建了一个子精灵,我希望它与父精灵一起移动但不能旋转它。我的场景确切地说,父精灵是一个风车,我正在旋转它,但是儿童精灵是一个风车栏末端的水桶。因此,当风车旋转时,我希望铲斗只是在那个动作中移动,现在它的移动和旋转看起来都是不切实际的。

以下是代码:

CGSize winSize = [CCDirector sharedDirector].winSize;

windmill = [CCSprite spriteWithFile:@"Chorki.png"];
windmill.position = CGPointMake(winSize.width*0.02f, winSize.height*0.56f);
windmill.scale = 0.55f;
[self addChild:windmill z:0];
//Add windmill sprite on screen

CCRotateBy *rot = [CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:5 angle: 360]];
    [windmill runAction:rot];
//The above code to rotate windmill, the next code is to add child sprite of bucket

CCSprite *bucket1 =  [CCSprite spriteWithFile:@"BucketBug.png"];
bucket1.position = ccp(235, 490.2f);
[windmill addChild:bucket1 z:-1]; 

编辑1:

抱歉,我忘了显示旋转精灵的代码。现在它已添加。

1 个答案:

答案 0 :(得分:1)

查看ccHonorParentTransform枚举:

/**
 Whether or not an CCSprite will rotate, scale or translate with it's parent.
 Useful in health bars, when you want that the health bar translates with it's parent but you don't
 want it to rotate with its parent.
 @since v0.99.0
 */
typedef enum {
    //! Translate with it's parent
    CC_HONOR_PARENT_TRANSFORM_TRANSLATE =  1 << 0,
    //! Rotate with it's parent
    CC_HONOR_PARENT_TRANSFORM_ROTATE    =  1 << 1,
    //! Scale with it's parent
    CC_HONOR_PARENT_TRANSFORM_SCALE     =  1 << 2,
    //! Skew with it's parent
    CC_HONOR_PARENT_TRANSFORM_SKEW      =  1 << 3,

    //! All possible transformation enabled. Default value.
    CC_HONOR_PARENT_TRANSFORM_ALL       =  CC_HONOR_PARENT_TRANSFORM_TRANSLATE | CC_HONOR_PARENT_TRANSFORM_ROTATE | CC_HONOR_PARENT_TRANSFORM_SCALE | CC_HONOR_PARENT_TRANSFORM_SKEW,

} ccHonorParentTransform;

通过CCNode的以下属性设置:

/** whether or not to transform according to its parent transfomrations.
 Useful for health bars. eg: Don't rotate the health bar, even if the parent rotates.
 IMPORTANT: Only valid if it is rendered using an CCSpriteBatchNode.
 @since v0.99.0
 */
@property (nonatomic,readwrite) ccHonorParentTransform honorParentTransform;

请注意,这仅适用于精灵位于CCSpriteBatchNode中的情况。由于我没有,我决定让父节点通过设置子节点的旋转等于其自身旋转的相反来更新子节点,从而保持子节点相对于世界的旋转不变。

// In a scheduled update function of the parent, see CCNode header for schedule functions
bucket.rotation = -self.rotation;