1
votes

I found out that the setopacity function does not work for one of our cocos2d games, it is using cocos2d 1.0.1. Not matter what value I set, the opacity of all ccnodes are always 255, and the fadein/fadeout actions are not working either. We have another game which is using the same version of cocos2d but that one works perfectly. Does anyone have any clue about how to solve this problem?

2
show us the code... also, what have you tried? Is setOpacity actually called? What node classes? Be specific.LearnCocos2D
I have setOpacity at multiple places and none of them is working, the opacity_ attributes in CCSprite are the correct value, but it does not have effect. So I guess there is something wrong with the library or I accidentally turned on some options which disabled the opacity setting.user1898387

2 Answers

2
votes

CCNodes don't actually have a texture (image), so there is no opacity property for them. I am assuming you think that setting the opacity of a CCNode will affect its children, which it would not. opacity only affects the texture of the object that you are setting the opacity for. You can set the opacity of a CCSprite, because it has a texture, but doing so would not affect that CCSprite's children. You would have to loop through all of the children, and set the opacity for each if you wanted to affect the opacity of more than one CCSprite.

0
votes

Basic DrawNode can't handle opacity by itself either (this feature is in the plan for cocos2d-4.*).

You can inherite your class from Node or DrawNode and implement setOpacity like this:

void AlphaNode::setOpacity(GLubyte opac) {
mOpacity = opac;
if (_bufferCount) {
    for (int i = 0; i < _bufferCount; i++) {
        _buffer[i].colors.a = mOpacity;
    }
}

if (_bufferCountGLPoint) {
    for (int i = 0; i < _bufferCountGLPoint; i++) {
        _bufferGLPoint[i].colors.a = mOpacity;
    }
}

if (_bufferCountGLLine) {
    for (int i = 0; i < _bufferCountGLLine; i++) {
        _bufferGLLine[i].colors.a = mOpacity;
    }
    _dirtyGLLine = true;
}
_dirty = true;
}

I think you can do something like this for Node.