axmolengine / axmol

Axmol Engine – A Multi-platform Engine for Desktop, XBOX (UWP) and Mobile games. (A fork of Cocos2d-x-4.0)
https://axmol.dev
MIT License
922 stars 205 forks source link

setOpacity is not working on the Node #1483

Closed HassamChaudhary1 closed 11 months ago

HassamChaudhary1 commented 11 months ago
  1. Create a Node
  2. Create a sprite and add this sprite in to node
  3. Use setOpacity function on the node and check the result, Opacity will not work on the node

NOTE: Opacity is working on the sprite. Issue is with the Node. It is also not working on the Axmol demo project

Test Code:

ax::Node* testNode = Node::create();
    testNode->setPosition(100, 100);
    Sprite* testSprite = Sprite::create("LaunchScreenBackground.png");
    testNode->addChild(testSprite);
    testNode->setOpacity(100);
    addChild(testNode);
rh101 commented 11 months ago

Your code is missing a call to setCascadeOpacityEnabled(). The cascading of the opacity value is false by default, so add this line to check if it fixes your issue:

testNode->setCascadeOpacityEnabled(true);

It is also not working on the Axmol demo project

What demo project? If you meant cpp-tests, then specifically which test are you referring to?

HassamChaudhary1 commented 11 months ago

This will work for newly created node but it will not work for a full project. I checked on cocos-2dx (previous) and their Cascading is off and opacity is working there without adding any additional code.

I can't use setCascadeOpacityEnabled on my project bcoz I updated my project and previously it was working Fine and now why I need to do setCascadeOpacityEnabled?

Yes cpp-tests. I added my own node to check bcoz All the tests are using setOpacity on Sprite or other component but not using directly on the node.

HassamChaudhary1 commented 11 months ago

Is there any way to not adding any additional line like testNode->setCascadeOpacityEnabled(true);

For more Info: If I set Opacity in Cocos2d-x editor of any node it also not reflect on the game.

rh101 commented 11 months ago

Yes cpp-tests. I added my own node to check bcoz All the tests are using setOpacity on Sprite or other component but not using directly on the node.

Any modification to the cpp-tests project should always be mentioned, since stating that the test project didn't work without any context confuses things.

Regardless of that, just because something works a certain way in Cocos2d-x doesn't mean that it was working correctly. You can check what has changed between Cocos2d-x v4 to Axmol to figure out why it, apparently, has different behavior.

HassamChaudhary1 commented 11 months ago

I got the point, I am going through all the places to find out the solution or reason of "Why we need to enable the cascading to use opacity on node?" If we need to set the cascading to enabled then why it's default value is false, why shouldn't we set it's value to true by default?

HassamChaudhary1 commented 11 months ago

Regardless of that, just because something works a certain way in Cocos2d-x doesn't mean that it was working correctly. You can check what has changed between Cocos2d-x v4 to Axmol to figure out why it, apparently, has different behavior.

I compared the both codes, code is similar, I'm confused when I use opacity on editor and then run my code with the updated .csb file, it also not shown opacity on the phone but when I call node->getDisplayedOpacity() it returns 0. On game UI, it's opacity seems 255.

rh101 commented 11 months ago

I'm confused when I use opacity on editor

What editor are you referring to? If it's Cocos Studio, then that editor hasn't been supported for many years by the Cocos team, and the code related to it is, technically, deprecated. I'm actually surprised the code to support it is still included in Axmol, but you'll have to go through that code and check if something has changed, or if there is some bug in there that is affecting your project.

HassamChaudhary1 commented 11 months ago

Yes It's Cocos Studio. So, As far as I understand, to use the opacity on node, we have to use setCascadeOpacityEnabled(true);, right?

rh101 commented 11 months ago

Yes It's Cocos Studio. So, As far as I understand, to use the opacity on node, we have to use setCascadeOpacityEnabled(true);, right?

The opacity of a specific node is set by setOpacity(), but, if you need that opacity to cascade to child nodes of that node, then yes, you need to call setCascadeOpacityEnabled(true); on that parent. That goes for any node that has child nodes, so if you have a tree of nodes, then you need to make sure all nodes with child nodes also have cascade opacity set to true.

rh101 commented 11 months ago

Refer to the discussion thread, this post, for a work-around. Please close this issue, since it's not a bug in Axmol.

halx99 commented 11 months ago

converted to discuss