想给游戏中所有的按钮添加点击音效。我的做法如下。

编辑器中报错如图。 运行没有问题。

麻烦大师以上王者分段左右的朋友积极回帖, 针对这个错误也可以,其他方案也可以。 期待大神。
creator 版本 ; 1.4.2
想给游戏中所有的按钮添加点击音效。我的做法如下。

编辑器中报错如图。 运行没有问题。

麻烦大师以上王者分段左右的朋友积极回帖, 针对这个错误也可以,其他方案也可以。 期待大神。
creator 版本 ; 1.4.2
看这报错像是递归调用、栈溢出导致的
![]()
这都干啥的
83行 cc.Node.prototype.on = function (type, callback, target, useCapture) {
84行 this.onClone(type, callback, target, useCapture);
都在第一张图里面,忘记截取行数了 sorry
两份脚本
一份音效:
sound-effect.js
cc.Class({
extends: cc.Component,
onLoad(){
this.node.on('touchend',()=>{cc.am.playSFX('btnClick.mp3')},this)
}
})
一份编辑器插件:
add-sound-effect-helper.js
cc.Class({
editors:{
executeInEditorMode: true,
},
properties:{
add:{
get:()=>{return false},
set:(value)=>{this.addSoundEffect(this.node.children);}
}
},
addSoundEffect(children){
for(let item of children){
let btn = item.getComponent(cc.Button);
if(btn){
item.addComponent('sound-effect');
}
let children = item.children;
if(!!children.length){
this.addSoundEffect(children);
}
}
}
});
把后一份脚本挂根节点然后激活add属性
util.cloneFunc有问题吧,不会是cc.Node.prototype.onClone === cc.Node.prototype.on 吧
感觉你的代码有些问题,if成立了就往events里push,直接调用button.sfxPlay();不就好了吗?
你这个需求

这样就成了吧
sxfName不能改到node下就获取button然后button.sfxPlay();
(写错别喷
)