最近界面要实现一个功能:在同一个ListVIEw中根据是否装备来展示宝石的列表,即在区分开来的两组宝石中间添加显示元素“--------------已装备的宝石-----------”,如下图。这个功能打我第一眼看到就觉得有点坑,网上并没有找到有效的答案,估计大部分是因为对问题的描述有偏差。虽然最后实现出来的时候感觉并没有多么复杂,但对于我刚入手Cocos2d-Js的一个新人来说,过程是有点艰难,因为我是一点点试验出来的。最后总结出来的无非是一些小技巧,现在分享给大家。当然,也会有更方便、快捷的方法,希望感兴趣的读者不吝赐教。
/** * 背包 * Created by xj on 2016/12/22. */InventoryCell = cc.tableVIEwCell.extend({ m_bagCell: null,m_inventory: null,m_level: null,m_image: null,m_Box: null,ctor: function () { this._super(); this.init(); },init: function () { if(this._super()){ this.m_bagCell = ccs.csloader.createNode(res.InventoryCell_Json); // this.m_bagCell.setAnchorPoint(0,0); this.addChild(this.m_bagCell); this.m_level = ccui.helper.seekWidgetByname(this.m_bagCell,"label_Level"); this.m_image = ccui.helper.seekWidgetByname(this.m_bagCell,"image_Equipment"); this.m_Box = ccui.helper.seekWidgetByname(this.m_bagCell,"ItemBox"); } return true; },draw: function (ctx) { this._super(ctx); }})InventoryUI = BaseUI.extend({ m_ListVIEw: null,m_tableVIEw: null,m_items: [],m_cellSize: null,m_label_Text: null,m_label_Capacity: null,m_label_filter: null,m_label_meltDown: null,m_btnFilter: null,m_btnCapacity: null,m_btnGemChoice: null,m_btnTabEquipment: null,m_btnTabPIEces: null,m_btnTabOther: null,m_btnMeltDown: null,m_type: 1,//1-4:装备、**、宝石、** m_flag: null,m_gemsNoEquip: [],m_gemsEquiped: [],ctor: function () { BaseUI.prototype.ctor.call(this); this.init(); },init: function () { if(!cc.Layer.prototype.init.call(this)) { return false; } this.m_items = DataManager._Eqps.slice(0); var objVIEw = ccs.csloader.createNode(res.Inventory_Json); this.addChild(objVIEw); this.m_cellSize = ccs.csloader.createNode(res.InventoryCell_Json).getContentSize(); this.m_label_Text = ccui.helper.seekWidgetByname(objVIEw,"label_Text"); this.m_label_Text.setString(Exceptions.TEXT_BAGCAPACITY); this.m_label_Capacity = ccui.helper.seekWidgetByname(objVIEw,"label_Capacity"); this.m_label_filter = ccui.helper.seekWidgetByname(objVIEw,"label_Filter"); this.m_label_meltDown = ccui.helper.seekWidgetByname(objVIEw,"label_MeltDown"); this.m_btnFilter = ccui.helper.seekWidgetByname(objVIEw,"btnFilter"); this.m_btnCapacity = ccui.helper.seekWidgetByname(objVIEw,"btnCapacity"); this.m_btnGemChoice = ccui.helper.seekWidgetByname(objVIEw,"btnTabItems"); this.m_btnTabEquipment = ccui.helper.seekWidgetByname(objVIEw,"btnTabEquipment"); this.m_btnTabPIEces = ccui.helper.seekWidgetByname(objVIEw,"btnTabPIEces"); this.m_btnTabOther = ccui.helper.seekWidgetByname(objVIEw,"btnTabOther"); this.m_btnMeltDown = ccui.helper.seekWidgetByname(objVIEw,"btnMeltDown"); this.m_btnFilter.addtouchEventListener(this.onbuttonClick,this); this.m_btnCapacity.addtouchEventListener(this.onbuttonClick,this); this.m_btnGemChoice.addtouchEventListener(this.onbuttonClick,this); this.m_btnTabEquipment.addtouchEventListener(this.onbuttonClick,this); this.m_btnTabPIEces.addtouchEventListener(this.onbuttonClick,this); this.m_btnTabOther.addtouchEventListener(this.onbuttonClick,this); this.m_btnMeltDown.addtouchEventListener(this.onbuttonClick,this); var ListVIEw = ccui.helper.seekWidgetByname(objVIEw,"tableList"); ListVIEw.setScrollbarEnabled(false); this.m_tableVIEw = new cc.CollectionVIEw(this,cc.size(ListVIEw.getContentSize().wIDth,ListVIEw.getContentSize().height)); this.m_tableVIEw.setDirection(cc.SCRolLVIEW_DIRECTION_VERTICAL); this.m_tableVIEw.x = ListVIEw.getpositionX(); this.m_tableVIEw.y = ListVIEw.getpositionY(); this.m_tableVIEw.setDelegate(this); this.addChild(this.m_tableVIEw); DataManager.addobserver(this,this.updateData,cc.NOTIFICATION_LAYER_FILTER,null); return true; },bindData: function (data) { this.m_label_Capacity.setString(this.m_items.length + "/" + "100"); },onbuttonClick: function (sender,type) { switch (type){ case ccui.Widget.touch_ENDED: if(sender.getname() == "btnFilter") { SceneManager.addLayer(cc.LAYER_EQUIPSELECTION); } else if(sender.getname() == "btnTabEquipment") { this.changeItems(1); } else if(sender.getname() == "btnTabPIEces") { this.changeItems(2); } else if(sender.getname() == "btnTabItems") { this.changeItems(3); } else if(sender.getname() == "btnTabOther") { this.changeItems(4); } else if(sender.getname() == "btnCapacity") { alert("额外格数"); } else if(sender.getname() == "btnMeltDown") { alert("meltDown"); } break; default: break; } },updateData: function (sender) { var filters = sender.getUserData(); this.m_items = DataManager._Eqps.slice(0); //根据filters[0,1]对m_items进行筛选 //多选 var filterItem = new Array(); if(filters[5] != 1){ //filters[5] == 1 全选 for(var i = 0; i < this.m_items.length; i++){ for(var j = 0; j < 5; j++){ if(filters[j] == 1 && this.m_items[i].Quality == j+1){ filterItem.push(this.m_items[i]); break; } } } this.m_items = filterItem; } //单选 //根据filters[0,1]对m_items进行筛选 // var filter = 0; //0-5:白绿蓝紫橙红 // for(var i = 0; i < 6; i++){ // if(filters[i] == 1){ // filter = i; // break; // } // } // if(filter != 5){ //filter = 5 全选 // for(var i = 0; i < this.m_items.length; i++){ // if(this.m_items[i].Quality != filter+1){ // this.m_items.splice(i,1); // i --; // } // } // } this.m_tableVIEw.reloadData(); },changeItems: function (type) { if(type == 1) { this.updatebuttons(true); this.m_items = DataManager._Eqps.slice(0); this.m_flag = null; } else if(type == 2) { this.updatebuttons(false); this.m_items = DataManager._Gems.slice(0); } else if(type == 3) { this.updatebuttons(false); var gems = DataManager._Gems.slice(0); this.m_gemsNoEquip = []; this.m_gemsEquiped = []; for(var i = 0; i < gems.length; i++){ if(gems[i].Amount > 0) { this.m_gemsNoEquip.push(gems[i]); } if(gems[i].EqpAmount > 0) { this.m_gemsEquiped.push(gems[i]); } } this.m_items = this.m_gemsNoEquip.concat(this.m_gemsEquiped); this.addNullCell(this.m_gemsNoEquip.length); } else if(type == 4) { this.updatebuttons(false); this.m_items = DataManager._Gems.slice(0); } this.m_type = type; this.m_tableVIEw.reloadData(); },updatebuttons: function (display) { this.m_label_filter.setVisible(display); this.m_label_meltDown.setVisible(display); this.m_label_Capacity.setVisible(display); this.m_label_Text.setVisible(display); this.m_btnFilter.setVisible(display); this.m_btnMeltDown.setVisible(display); this.m_btnCapacity.setVisible(display); },selectedItemEvent: function (sender,type) { switch (type){ case ccui.ListVIEw.EVENT_SELECTED_ITEM: alert(sender.getCurSelectedindex()); break; default: break; } },tableCellSizeforIndex: function (table,IDx) { if(this.m_type == 3 && (IDx > this.m_flag-5 && IDx <= this.m_flag)) { return cc.size(110,25); } return this.m_cellSize; },numberOfCellsIntableVIEw: function () { if(!this.m_items){ return 0; } return this.m_items.length; },cellSizefortable: function (table) { return table.getContentSize(); // return cc.size(580,130); },tableCellAtIndex: function (table,IDx) { var itemAtt = this.m_items[IDx]; var cell = null; // var cell = table.dequeueCell(); if(!cell){ cell = new InventoryCell(); } cell.m_inventory = itemAtt; if(itemAtt.ID != 0){ var pic = ""; if(this.m_type == 1) { cell.m_level.setString("Lv." + itemAtt.Level); pic = "res/pic/Equip/" + itemAtt.IconID + ".png"; } else if(this.m_type == 2) { cell.m_level.setString(itemAtt.Amount); pic = "res/pic/Gems/" + itemAtt.IconID + ".png"; } else if(this.m_type == 3) { if(IDx < this.m_gemsNoEquip.length) { cell.m_level.setString(itemAtt.Amount); } else { cell.m_level.setString(itemAtt.EqpAmount); } pic = "res/pic/Gems/" + itemAtt.IconID + ".png"; } else if(this.m_type == 4) { cell.m_level.setString(itemAtt.Amount); pic = "res/pic/Gems/" + itemAtt.IconID + ".png"; } } else { if(IDx == this.m_flag-4) { cell = new CombatCell(); cell._Time.setVisible(false); cell._Info.setString(" ------已装备宝石------") } else { cell.m_level.setVisible(false); cell.m_image.setVisible(false); cell.m_Box.setVisible(false); } return cell; } this.changeNodeTexture2(pic,"image_Equipment",cell); return cell; },tableCelltouched: function (table,cell) { if(cell.m_inventory.ID != 0){ if(this.m_type == 1) { // SceneManager.addLayer(cc.LAYER_EQUIPUI,cell.m_inventory); var ui = SceneManager.addLayer(cc.LAYER_EQUIPUI,null,true,cc.LAYER_ANI_NONE,true); if (ui) { //替换装备与装备相关 *** 作 ui.initEquipUI(null,cell.m_inventory); } } else if(this.m_type == 3) { SceneManager.addLayer(cc.LAYER_GEMINFO,cell.m_inventory); } } },addNullCell: function (index) { //添加空Cell var flag = index % 5 == 0 ? index+5 : 5 * (parseInt(index/5) + 2); var nullCell = new GemAtt(); //从索引处开始添加空元素 for(var i=index; i<flag; i++) { this.m_items.splice(i,nullCell); if(i == flag-1) { this.m_flag = i; } } }});InventoryUI.create = function () { return new InventoryUI();}
思路解析:
把整理好的没有装备的宝石和已经装备好的宝石数组放入同一数组中,使其前一部分全为没有装备的宝石,后一部分则为装备的宝石。根据分界线的索引,即没有装备宝石的数组的length属性,为整合好的数组添加没有实际意义的Cell,把它看作填空Cell,使得填空后的数组满足:当未装备的宝石数量不为每行所能含有Cell的个数的整数倍时,最后一行填充空的Cell。以让用户感觉后边没有元素了。比如:CollectVIEw每行可容纳5个Cell,当未装备的宝石为8个时,需要在会后一行,填充两个空的Cell。此时做到可以把装备与否的宝石区分开来。在此基础上,再添加每行容纳Cell个数的空Cell,即可将要求中的提示信息“--------已装备宝石----------”留出空间来。最后处理留出空间的高度显示。
关键代码解析:
1、行数:196-212
var gems = DataManager._Gems.slice(0); this.m_gemsNoEquip = []; this.m_gemsEquiped = []; for(var i = 0; i < gems.length; i++){ if(gems[i].Amount > 0) { this.m_gemsNoEquip.push(gems[i]); } if(gems[i].EqpAmount > 0) { this.m_gemsEquiped.push(gems[i]); } } this.m_items = this.m_gemsNoEquip.concat(this.m_gemsEquiped); this.addNullCell(this.m_gemsNoEquip.length);
将拿到的宝石结构,分成没有装备和已经装备的两个数组,m_gemsNoEquip和m_gemsEquiped;并通过concat方法将两个数组合成一个新的数组,之后执行addNullCell()方法,为整合好的数组,添加空Cell个数;
2、行数:334-347
addNullCell: function (index) { //添加空Cell var flag = index % 5 == 0 ? index+5 : 5 * (parseInt(index/5) + 2); var nullCell = new GemAtt(); //从索引处开始添加空元素 for(var i=index; i<flag; i++) { this.m_items.splice(i,nullCell); if(i == flag-1) { this.m_flag = i; } } }
此为addNullCell()的方法,此方法在为数组添加填充Cell的同时,在344行标记了最后一个空Cell元素的索引,这个索引将是一个关键;
3、行数:240-246
tableCellSizeforIndex: function (table,根据索引值,将整合的数组中整行为空的高度设置成需要的数值,如:正常Cell的高度是130,此处将空行设置为25;
4、行号:299-311
if(IDx == this.m_flag-4) { cell = new CombatCell(); cell._Time.setVisible(false); cell._Info.setString(" ------已装备宝石------") } else { cell.m_level.setVisible(false); cell.m_image.setVisible(false); cell.m_Box.setVisible(false); } return cell;在 tableCellAtIndex中,如果检测到是数组中的填充数据,那么可以把此处的cell替换成其他cell,此处用了一个高度较低,宽度较长的适合显示的Cell。至于为什么IDx==this.m_flag-4,这个位置根据具体情况设置。从代码中可以看出其实填充Cell就是把普通Cell的控件设置成不可见。
实现效果:
总结以上是内存溢出为你收集整理的Cocos2d-JS中CollectionView实现不同大小Cell同时显示的技巧(ListView中添加显示标签)全部内容,希望文章能够帮你解决Cocos2d-JS中CollectionView实现不同大小Cell同时显示的技巧(ListView中添加显示标签)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)