您的位置:首页 > 其它

背包系统的制作

2016-08-04 08:44 405 查看
在一款RPG游戏中购买装备是必须实现的,创建背包系统的时候需要注意的就是。创建背包时必须区分背包的ID和道具配置表的ID。设置背包ID的作用就是区别当前点击的道具的唯一性。给所以背包的属性都存在一个数组中。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: