javascriptcocos2d-xcocoscreator

How to add item to top of ScrollView children?


In Cocos Creator I've a ScrollView and access its content like this:

Content: {
    default: null,
    type: cc.Node,
},

I want to add a new item on top of the children list, when I do:

var item = cc.instantiate(this.itemPrefab);
this.Content.insertBefore(item, this.Content.children[0]);

I get an error that object doesn't support property or method insertBefore.


Solution

  • I have looked at some of the documentation,

    ( https://cocos2d-x.org/reference/html5-js/V3.7/symbols/ccui.ScrollView.html )

    ( https://cocos2d-x.org/reference/html5-js/V3.0alpha/symbols/cc.Node.html )

    Would I be right in thinking that 'Content' is a ccNode being passed to Scrollview?

    The closest methods I could find on ccNode were addChild and reorderChild. They both support a zOrder method which determines the order in which they are drawn. It seems possible that it might affect its order in its parents array.

    (Assuming the other items have a zOrder > 0):

    You could try,

    var item = cc.instantiate(this.itemPrefab);
    this.Content.addChild(item, 0);
    

    Hopefully it works and you don't have to delete the whole array!