How to create child in an outside function?

I created an outside function like outside ctor func for

ex
var spritecreate=function(sender,type){

var sprite=cc.Sprite(“res.sprite_png”)
sprite.attr({
x: 400,
y: 300,
scale: 0.5
)};
this.addChild(sprite,1);

}
it is showing an error this.addChild is not a function

please help

i need to call this function on a onTouchEnded event
thanks

Try it:

var TestSprite = cc.Node.extend({
    onEnter: function(){
        this._super();
        cc.eventManager.addListener({
            swallowTouches: true,
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            onTouchBegan: function () {
                return true;
            },
            onTouchEnded: function (touch, event) {
                var target = event.getCurrentTarget();
                
                target.inFunction();
                // or
                outFunction(target);
            }
        }, this);
    },
    inFunction: function(){
        //create sprite ...
        var spr = new cc.Sprite("....");
        this.addChild(spr);
    }
});
var outFunction = function(parent){
    //create sprite ...
    var spr = new cc.Sprite("...");
    parent.addChild(spr);
};
1 Like

Thank u soo much…!!