In cocos2dx-lua, if you want to set the transparency of parent and child nodes, you can use the setCasCadeOpacityEnabled method of the parent node to turn on the transparency of the node, otherwise the transparency of the node cannot be set. However, there is a drawback to this method. When there are multiple levels of children in a node, the transparency of the children of the children cannot be turned on. A common solution on the web is to change the engine source code and set the node’s transparency to on by default. Here’s another solution.

Recursion turns on opacity of child nodes

In order to enable the transparency of all the children of the node, we recursively traverse all the children of the node, because the node hierarchy does not actually have many layers (too many layers can be troublesome to pull out some operations), so we usually do not have to worry about stack overflow. First affixed source code, easy to introduce (has been written tool class convenient call) :

local SetOpacityUtil = class("SetOpacityUtil") function SetOpacityUtil:ctor(node) self.children = {}; self.node = node; end -- -- @description: First, recursively traverse the child nodes, insert the child nodes into the table, -- if there is no child node or has traversed the node in the process of traversal, then turn on -- transparency setting, the node with enabled setting is removed from the table, and the recursion is ended when the table is empty. -- function setOpacityUtil :_setCascadeOpacity() if #self.children == 0 then return nil end self.children[#self.children].node:getChildren() or self.children[#self.children].isForEach then self.children[#self.children].node:setCascadeOpacityEnabled(true) table.remove(self.children, #self.children) end if #self.children == 0 then return nil end If self.children[#self.children]. Node :getChildren() and not self.children[#self.children] then self.children[#self.children].isForEach = true for _, child in ipairs(self.children[#self.children].node:getChildren()) do table.insert(self.children, {node = child, isForEach = false}) end end return self:_setCascadeOpacity() end -- -- @Author: Y.M.Y -- @description: Recursion opens the child nodes of the UI node set transparency option - function SetOpacityUtil: setCascadeOpacity for _ (), v in pairs(self.node:getChildren()) do table.insert(self.children, {node = v, isForEach = false}) self._setCascadeOpacity() end self.node:setCascadeOpacityEnabled(true) end return SetOpacityUtil

The code is implemented as follows:

  • First, push a child node of the root node onto the stack.
  • If the node has no children or has traversed the node, the transparency setting of the node will be turned on directly and the stack will be out.
  • If a node has children and is not marked as traversed, it traverses all the children of the node and pushes them on the stack one by one.
  • Repeat steps 2 and 3 until the stack is empty.
  • The next child node is pushed on the stack and the operation is repeated until all the children of the root node are traversed.