背包系统的制作

本文介绍了一款RPG游戏中背包系统的制作过程,强调了区分背包ID和道具配置表ID的重要性。创建背包时,将所有属性存储在数组中,包括背包ID、道具种类ID和数量。通过遍历属性并加载预制件来初始化背包,同时实现选中和非选中状态的切换。选中方法中,利用逻辑锁控制选择框的显示状态,并通过自定义事件处理选中与取消选中的交互逻辑,确保每次只能有一个道具被选中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一款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是否相同,如果不相同并且当前的为选中状态。就再次调用选中的方法。就可以实现点击一个道具之后再选中另一个道具的时候。取消之前的选中到当前选中上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值