CCSprite Fadeout with children

社会主义新天地 提交于 2020-01-11 09:18:10

问题


I'm using a CCSprite with a few other CCSprite objects added as children, the other animations I'm using (scale and rotate) work great and the children are animated too. But when I'm using CCFadeOut, it only fades the parent.

I've read that fadeout doesn't apply to the children. Is there any way other than iterating over every child and calling the fadeout on each of them?


回答1:


This answer is rendered obsolete by Gregory Johnson Answer


Well, I guess your choices are (Ranked from simplest to complex):

1) Just go into the CCSprite class in cocos2d library, and hack it. (<3 open source). (not recommended).

-(void) setOpacity:(GLubyte) anOpacity
{
opacity_ = anOpacity;

// special opacity for premultiplied textures
if( opacityModifyRGB_ )
    [self setColor: colorUnmodified_];

    [self updateColor];

    for (id<CCRGBAProtocol> child in children ) {
        // You should check if child responds to selector or conforms to CCRGBAProtocol.
        [child setOpacity:opacity];
    }
}

2) Same as the solution above, except subclass CCSprite to MyCCSprite, and inherit from it instead of CCSprite. Finally, override setOpacity: in the new class:

- (void) setOpacity:(GLubyte)opacity
{
    [super setOpacity:opacity];
    for(id<CCRGBAProtocol> child in children) {
        [child setOpacity:opacity];
    }
}

3) Run the CCFade action on the parent and the children by iterating them. (silly, if you ask me).

IMPORTANT: Just please, please, please keep in mind that opacityis a property of the CCRGBAProtocol. Not all CCNode classes have it. So, make sure you keep that in mind.

References:

  1. http://www.cocos2d-iphone.org/forum/topic/1252



回答2:


As of Cocos2d ver. 2.1, CCNodeRGBA has a "CascadeOpacity" BOOL property. Set it to YES on the parent CCSprite to fade out the children nodes as well as the parent.



来源:https://stackoverflow.com/questions/12409697/ccsprite-fadeout-with-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!