新手求助,销毁动态添加的button

var item = cc.instantiate(this.prefabRankItem);
item.getComponent(‘baopaiItem’).init(this.inputstr.string);
var deleteButt.node.on(cc.Node.EventType.TOUCH_END, this.deletepunish.bind(this));
this.scrollView.content.addChild(deleteButt);
用上述方法在scrollView.content中动态添加了4个button,都是用prefab创建的,想要在点击任意一个button后,直接调用deletepunish函数把它销毁
deletepunish: function () {
var thisn = cc.find(‘Canvas/New ScrollView/view/content’);
},
得到content的node后,怎么才能知道点击的是哪个BUTTON呢?

问题困扰2天了,看了好多帖子和教程还是没有解决! 求大神,万分感谢!

baopaiItem 这里面可以设置一个index,然后在init的时候传入

1赞

cc.Class({
extends: cc.Component,
properties: {
parentId: 0,
},
init: function (data) {
this.parentId = data.parentId;
},
deletepunish: function () {
this.parentId==?
}
}

如果你是预设,并且这个baopaiItem脚本绑定的是预设上,那么调用到deletepunish就是当前的

1赞

如果四个button的回调都是这个函数,那么直接像下面写

deletepunish: function (event) {
event.target.destroy();
},

这样的话,哪个button触发的事件,就销毁哪个button,如果你要获得button的话,可以通过父节点获得,假设所有button的父节点都是content,并且四个button的节点名不相同

content.getChildByName(“button的名称”);

如果你是要点击任意一个按钮销毁所有button,直接

content.removeAllChildren(true);

1赞

谢谢楼上两位的解答,已经解决了!
我把deletepunish写入baopaiitem的脚本里了,直接构建点击即销毁的prefab,明明有条直路居然拐了这么多个弯~~
我再去试试两位提供的方法,万分感谢!!!