Cocos2D帮助:如何旋转儿童精灵与父的子精灵只移动和不旋转? [英] Cocos2D help: How to rotate child sprite with the parent where child sprite only moves and does not rotate?

查看:172
本文介绍了Cocos2D帮助:如何旋转儿童精灵与父的子精灵只移动和不旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我创建了一个父sprite永远旋转。我已经创建了一个孩子精灵到它,我想与父精灵一起移动,但不旋转它。我的情况恰恰是,父精灵是一个风车,我正在旋转它,但孩子精灵是一个桶在一个风车吧的尽头。因此,当风车转动时,我想要的桶只是移动的运动,现在它的移动和旋转看起来不现实。

My problem: I have created a parent sprite which rotates forever. I have created a child sprite to it which I want to move along with the parent sprite but not rotate it. My scenario exactly is that the parent sprite is a windmill and I am rotating it, but the child sprite is a bucket at the end of one windmill bar. So when the windmill rotates, I want the bucket to just move in that motion, where as now its moving and also rotating which looks unrealistic.

这里是代码: / p>

Here is the code:

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:

对不起,我忘了显示代码来旋转sprite。现在添加了。

Sorry I forgot to show the code to rotate the sprite. Now its added.

推荐答案

查看ccHonorParentTransform枚举:

Take a look at the ccHonorParentTransform enum:

/**
 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的以下属性设置:

It is set through the following property of 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;

请注意,这只有在sprite在CCSpriteBatchNode中时才有效。因为我的不是,我决定让父节点更新子节点通过设置孩子的旋转等于自己的旋转相反,从而保持孩子的旋转相对于世界恒定。

Note that this will only work if the sprites are in a CCSpriteBatchNode. Since mine was not, I decided to let the parent node update the child node by settings the rotation of the child equal to the opposite of its own rotation, thus keeping the child's rotation relative to the world constant.

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

这篇关于Cocos2D帮助:如何旋转儿童精灵与父的子精灵只移动和不旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆