Cocos Studio exports a scene or UI layout as a CSB file. Cocos2d-x 3.17 Lua can load the node tree through cc.CSLoader, after which named controls can be found and bound to callbacks.
Load a layout directly
1 | local root = cc.CSLoader:createNode("ui/MenuScene.csb") |
For nested controls, either follow the hierarchy with repeated getChildByName calls or use a deliberate recursive lookup helper. Duplicate names make recursive lookup ambiguous, so use unique names for controls that code accesses.
Touch listeners can inspect the event type:
1 | startButton:addTouchEventListener(function(sender, eventType) |
Load a timeline
1 | local timeline = cc.CSLoader:createTimeline("ui/MenuScene.csb") |
The animation name must match the Cocos Studio export.
A reusable view base
A project-level ViewBase can define RESOURCE_FILENAME, create the CSB root, expose a binding phase, and centralize lifecycle cleanup. Derived views should declare their resource and bind only the controls they own. Fail early when a required node is missing instead of storing nil and crashing much later.
Keep CSB names and code changes in the same review, avoid business logic inside the loader, and remove event listeners or scheduled callbacks when the view exits.