背包系统的制作
2016-08-04 08:44
405 查看
在一款RPG游戏中购买装备是必须实现的,创建背包系统的时候需要注意的就是。创建背包时必须区分背包的ID和道具配置表的ID。设置背包ID的作用就是区别当前点击的道具的唯一性。给所以背包的属性都存在一个数组中。
这里是创建一个背包的基本属性。属性包括背包的ID值。还有道具的种类ID还有就是道具的数量。
开始创建背包。遍历背包中所有的属性。
加载预制件并且调用预制件脚本里面的方法。设置道具的基本信息。
在背包系统中涉及到选中和非选中的两个状态。在这个背包系统中在与预制件中设置了一个外部的选择框默认是隐藏的状态。在加载预制件的时候设置选中框的active属性为false。封装成一个选中方法。当点击之后会调用这个方法。方法里面先是给选中的逻辑锁取反。如果当前是false取反就是true。然后让选中框的active的属性等于逻辑所。这样就实现了第一次点击选中。再点击取消选中的功能。当一个道具被选中之后就抛出一个自定义事件。抛出的是背包的ID值。并且在同一个类中对这个事件进行处理。接到这个当前背包ID值判断时候和现在点击的背包ID是否相同,如果不相同并且当前的为选中状态。就再次调用选中的方法。就可以实现点击一个道具之后再选中另一个道具的时候。取消之前的选中到当前选中上。
4000
GameAction.addItem(4,1); //----------------------------------- addItem:function(itemId,itemNum){ var that = this var newItem = { id:(that._itemId++), type:itemId, num:itemNum, } GameData.bag.push(newItem) },
这里是创建一个背包的基本属性。属性包括背包的ID值。还有道具的种类ID还有就是道具的数量。
//开始遍历背包数组调用加载预制件方法 pub_beganloadProp:function(){ var ob = GameData.bag for(var index in ob){ this._beganloadpropPfb(ob[index]); } },
开始创建背包。遍历背包中所有的属性。
//加载预制件方法 _beganloadpropPfb:function(ob){ var that = this; this.oldweapon = ob.type; cc.loader.loadRes("prefab/prop", function (err, propprefab) { var newPropNode = cc.instantiate(propprefab); //that._addSpriteFrameToContainer(newPropNode.getChildByName('propSprite').getComponent(cc.Sprite), ob.pic); newPropNode.getComponent("itme").pub_setPfbInfo(ob); that.propLayer.getChildByName('laoutNode').addChild(newPropNode); }) },
加载预制件并且调用预制件脚本里面的方法。设置道具的基本信息。
//设置预制件信息 pub_setPfbInfo:function(ob){ this._data = ob; this.typeId = ob.type; this.node.getChildByName('numLabel').getComponent(cc.Label).string = this._data.num; this._addSpriteFrameToContainer(this.node.getChildByName('propSprite').getComponent(cc.Sprite), GameData.prop[ob.type].pic); this.node.on(cc.Node.EventType.TOUCH_START,this._setSelectHand.bind(this)); //------------------------------------接收自定义事件--------------------------------------------------------- var that = this; var node = cc.find("UIScript") node.on(GameEvent.propAction,function(event){ var data = event.getUserData()[0]; if(data != that._data.id && that.Select){ that._setSelectHand(); } }) //----------------------------------更新显示-------------------------------------------------------------------- node.on(GameEvent.useItemAction,function(event){ var data = event.getUserData(); if(data==that._data.id){ that._data.num--; that.node.getChildByName('numLabel').getComponent(cc.Label).string = that._data.num; if(that._data.num <= 0){ that._data.num = 0; GameData.bag.splice(GameData.bag.indexOf(that._data.id),1); //cc.log("xxxxxxxxx",that._data.id-1,GameData.bag) that.node.removeFromParent(true); node.getComponent("prop").removeInfo(); } } }) }, //选择状态的方法 _setSelectHand:function(){ this.Select = !this.Select; this.node.getChildByName('cheoseSprite').active = this.Select if(this.Select){ var proptypeId = new cc.Event.EventCustom(GameEvent.propAction, false) proptypeId.setUserData([this._data.id,this.typeId,this._data.obId]); cc.log(this._data.id,"------------------------------------") cc.find('UIScript').dispatchEvent(proptypeId); cc.find('UIScript').getComponent("prop").setbuybtn(this._data); } },
在背包系统中涉及到选中和非选中的两个状态。在这个背包系统中在与预制件中设置了一个外部的选择框默认是隐藏的状态。在加载预制件的时候设置选中框的active属性为false。封装成一个选中方法。当点击之后会调用这个方法。方法里面先是给选中的逻辑锁取反。如果当前是false取反就是true。然后让选中框的active的属性等于逻辑所。这样就实现了第一次点击选中。再点击取消选中的功能。当一个道具被选中之后就抛出一个自定义事件。抛出的是背包的ID值。并且在同一个类中对这个事件进行处理。接到这个当前背包ID值判断时候和现在点击的背包ID是否相同,如果不相同并且当前的为选中状态。就再次调用选中的方法。就可以实现点击一个道具之后再选中另一个道具的时候。取消之前的选中到当前选中上。
4000
相关文章推荐
- 用NGUI制作背包系统
- 虚幻引擎UE4背包系统(如何制作可拖动(Drag and Drop)的背包(Scrollbox))
- (CEGUI)DragDropDemo例子的延伸(添加,删除,双击ITEM) -------制作背包系统
- Unity3d制作游戏背包系统
- NGUI制作背包系统
- 用Java制作网络文件下载系统
- VC编程制作系统托盘程序
- 精简版系统制作的关键技术
- 网吧xp母盘制作流程+QQDLL+系统文件DLL修改
- VS2005制作安装包的“系统必备”选项
- 系统启动盘制作的注意事项
- VC编程制作系统托盘程序
- 制作基于软盘的Linux系统
- Asp.net+Xml:实现制作一个完整的新闻系统
- 图像合成,做一个类似制作大头贴的系统
- 转贴:个性化的Blog Skin制作方法详解(本教程适用于.Text的Blog系统)
- 万能Ghost系统制作教程
- 前公司在制作播客系统(Web程序)中,用到从视频截图功能.
- 游戏音乐制作的软件系统
- 制作基于软盘的Linux系统