@745872343 写道:
循环创建预制体的实例(按钮),之后用获取动态资源设置按钮的图标,但只有最后一个按钮设置图片成功。感觉是异步加载资源时,再次调用会中断前一次,所以只有最后一次成功。不知如何解决,求大神指点~
代码如下:
//创建按钮 createFaceItem: function () { for (var i = 0; i < 18; i++) { if (i % 6 == 0) { this.facePageView.addPage(this.createPage()); } var pages = this.facePageView.getPages(); var page = pages[parseInt(i / 6)]; var newFaceItem = cc.instantiate(this.faceItem); var clickEventHandler = new cc.Component.EventHandler(); clickEventHandler.target = this.node; //这个 node 节点是事件处理代码组件所属的节点 clickEventHandler.component = "pluginItemControl" clickEventHandler.handler = "onBtnClicked"; // clickEventHandler.customEventData = "foobar"; //传递参数 var button = newFaceItem.getComponent(cc.Button); button.clickEvents.push(clickEventHandler); //设置按钮图标 cc.loader.loadRes(resList.pluginMicrophoneIcon, cc.SpriteFrame, function (err, spriteFrame) { newFaceItem.getChildByName('face').getComponent(cc.Sprite).spriteFrame = spriteFrame; }); newFaceItem.tag = i; newFaceItem.setPosition(cc.p(0, 0)); newFaceItem.width = sceneFit.faceItemSize.width; newFaceItem.height = sceneFit.faceItemSize.height; page.addChild(newFaceItem); } },
帖子: 3
参与者: 1