How to avoid massive Scene files?

Hello guys.

Whenever I develop games using Cocos2D-X, I end up with XxxScene.cpp files with 1000+ lines of code.

I try to break up the codes in multiple files by creating Node subclasses like HudNode, GameNode etc. These Nodes act like containers as I add components like buttons, sprites, labels etc as children to them. Finally I add the Node subclasses to the Scene and use methods and callbacks to communicate.

Example:

HomeWidgetsNodeDelegate.h

class HomeWidgetsNodeDelegate {
public:
    void onHomeWidgetsNodeFinishEntranceAnimation(HomeWidgetsNode* sender) = 0;
    void onHomeWidgetsNodeFinishExitAnimation(HomeWidgetsNode* sender) = 0;
    void onHomeWidgetsNodeClickPlay(HomeWidgetsNode* sender) = 0;
}

HomeWidgetsNode.h

class HomeWidgetsNode : public cocos2d::Node {
public:
    HomeWidgetsNodeDelegate* delegate;

    void animateEntrance();
    void animateExit();

private:
    cocos2d::ui::Button* _playButton;
    cocos2d::ui::Text* _bestScoreText;

    void createPlayButton();
    void createBestScoreText();

    void onClickPlay(cocos2d::Ref* sender);
}

HomeScene.h

class HomeScene : public cocos2d::Scene, public HomeWidgetsNodeDelegate {
private:
    HomeWidgetsNode* _widgetsNode;

    void createWidgetsNode() {
        _widgetsNode = HomeWidgetsNode::createWithSize(getContentSize());
        addChild(_widgetsNode);

        _widgetsNode->delegate = this;
    }
}

I am concerned about the performance drop of this approach, as I need to create the extra container Nodes. Is there any better approach to break up the massive Scene.cpp files? Any other helpful tricks to improve code quality?

Thanks in advance.

I do things pretty much like you do. My Scene is assembled from other classes brought together.

My Scene files look just like yours. I’m not sure there’s a lighter way of writing it really.

I don’t know about performance specifically, but the only time I’ve ran into a performance issue from loading a scene in my game was because I was loading in hundreds of complex Nodes (15+ children each), and I resolved that by lazy loading them in.

In terms of code reduction, I’ve had a lot of success with first using Cocos Creator (or Studio, if you’re pre 3.10 like I was for a long while) to offset the positioning and creation of the Nodes.

I don’t store nodes as members unless I need to do it, because it’s usually unnecessary, and the first time I need one, I just getChildByName after. I wrote a forum post about some of the helper functions I’ve got, but the most commonly used ones is bind_touch_ended(Widget, callback) to bind touch ended on a ui::Widget, and a bunch of macros to save me dynamically casting Nodes into ui::Buttons or whatever.

I’ve got helper functions overloaded to easily apply aliasing for my pixel art game, I’ve got a function, prep_button(Button) that sets fonts and backgrounds for a given button. The source for bind_touch_ended will have to be edited on your codebase though because I forked all of ui::Widget to pass down the actual Touch object to onTouchEnded(), which I was surprised it didn’t do already.

tl;dr I write my scenes the same way, here’s a bunch of helper funcs I’ve got to save me repeating myself.

3 Likes

Everything seems fine to me. I don’t think you are going to have any performance issues.