I want to add a sprite2 to sprite1, scale the width of of sprite 1 without scaling sprite2.
I found the code below part of the Cocos2d api; CCSprite.h line 54, but I don't开发者_运维知识库 know how to use it nor what the "1<<2" means.
Basically, I'm doing the following but it's not working:
[self addChild: sprite1];
[sprite1 addChild: sprite2]
sprite1.scaleX = 2;
sprite2.CC_HONOR_PARENT_TRANSFORM_SCALE = false;???
Yeah not sure how to use the enum.
thank you
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,
//! 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,
} ccHonorParentTransform;
<< - is a bit operation of a shift (my native language is russian and i've translated as is - not sure it's correct). But it is not required for you to understand how it work in this situation because in this case it's just a method to fill the enum values.
From cocos2d documentation
- (ccHonorParentTransform) honorParentTransform [read, write, assign]
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.
Are you using batch rendering ?
EDIT:
This line is very strange (doesn't it give a warning?)
sprite2.CC_HONOR_PARENT_TRANSFORM_SCALE = false
You should write
sprite2.honorParentTransform &= ~CC_HONOR_PARENT_TRANSFORM_SCALE;
PS: The enum is created using bit operations because it's give you the ability to misc the configuration. For example you can write
sprite2.honorParentTransform &= ~(CC_HONOR_PARENT_TRANSFORM_SCALE | CC_HONOR_PARENT_TRANSFORM_ROTATE);
It will enable both translate and rotate So the honorParentTransform is a bitmask, that allows you to configure it's configuration - not only use some predefined values but also use there combinations.
Here you can write more about bitwise operations http://www.cprogramming.com/tutorial/bitwise_operators.html
In our case is happening something like this:
You have a current mask for example 01101111
(it is 32 bit really)
and CC_HONOR_PARENT_TRANSFORM_SCALE is something like this 00001000
- it have only one nonzero bit. ~
- is inversion: so it transform 00010000
to 11101111
and then you make the bitwise addition with you current mask - so all the bits will be preserved except the forth one!
精彩评论