GetChildByName & GetComponent as fields

Hi! I’ve been using CocosCreator for a few weeks now and I’m quite enjoying it, but there’s a question I have to ask devs.

Have you considered making node’s children and components accessible as named fields?

Cumbersome constructions like

charNode.getChildByName("portrait").getChildByName("hpBar").getComponent(cc.Sprite)

would turn into neat

charNode.portrait.hpBar.Sprite

It would impose more strict naming rules for nodes, but personally I think it is totally worth it.
Is it technically feasible? I’m new to JavaScript, but it’s absolutely doable in Lua, and they look pretty similar for me now :smile:
Or am I missing some code design concerns here?

Hi, by default, we haven’t expose all children names or components as property of a Node, for most of the time, it wouldn’t be necessary. But indeed user will need a easy access to some of them and use them very frequently, in this case, we suggest you to expose your own property, there are a few ways to do it:

// Simplest
charNode.portrait = charNode.getChildByName("portrait");
// More structured, expose them with a component
cc.Class({
    name: 'EasyAccess'
    extends: cc.Component,

    properties: {
        portrait: cc.Node,
        hpSprite: cc.Sprite
    }
});

Then you can link the needed portrait node and Sprite component of hpBar with this Component, and access them like this

var easyAccess = charNode.getComponent('EasyAccess');
easyAccess.hpSprite;
easyAccess.portrait;