19.Cocos跑酷游戏——06添加奖励物品

1.List工具篇
2.工具篇 Dictionary
3.工具篇 读取Json文件保存数据
4.资源管理ResourceManager
5.界面层级管理 LayerManager
6.界面管理 UIManager
7.事件监听篇 EventBus
8.枚举篇 枚举管理
9.游戏总管 Mir
10游戏入口 Main
11.声音管理器
12首页界面
13.游戏界面
14.01 背景
15.02主角(与游戏界面交互)
16.03添加怪物来袭
17.04添加障碍物
18.05 添加障碍物排列
19.06添加奖励物品
20.07奖励物质排列数据
21.从零开始-Cocos跑酷游戏——游戏结束界面
22.最后的补充

添加奖励物品,进行积分操作,提高游戏的可玩性

配置表

[
  {
    "id": 1001,
    "path": "xingxing1",
    "score": 1,
    "name": "星星1"

  },
  {
    "id": 1002,
    "path": "xingxing2",
    "score": 2,
    "name": "星星2"

  },
  {
    "id": 1003,
    "path": "xingxing3",
    "score": 3,
    "name": "星星3"
  }
]

配置表数据

function RewardItemCfgVo()
{
    this.id;
    this.name;
    this.path;
    this.score;
}
RewardItemCfgVo.prototype.SetValue=function(mData)
{
    this.id=mData.id;
    this.name=mData.name;
    this.path=mData.path;
    this.score=mData.score;
}
module.exports=RewardItemCfgVo;

读取保存配置表数据

var BarrierSortCfgData=
{
    dataList:null,
    filePath:null,
    init:function()
    {
        this.dataList=new window.List();
        this.filePath=window.Constant.RootPath.CONFIG_ROOT_PATH+"BarrierSort";
        window.cc.loader.loadRes(this.filePath,(function(err,array)
        {
            if(err)
            {
                console.log("错误信息:"+err);
                return;
            }
            var arrs=array.json;
            for(var i=0;i<arrs.length;i++)
            {
                var mData=arrs[i];
                var mVoData=new window.BarrierSortCfgVo();
                mVoData.SetValue(mData);
                this.dataList.add(mVoData);
            }
        }).bind(this));
    },
    GetBarrierSortCfgData:function()
    {
        return this.dataList;
    }
}
module.exports=BarrierSortCfgData;

奖励物质数据处理与管理

var RewardItemData=
{
    dataList:null, //生产出的星星
    dataAndPefabDic:null,  // key 对应的预制体
    dataDictiony:null,     //数据 对应的对象池
    prefab:cc.Prefab,
    loadPath:null,
    initCount:50,// 初始化个数
    count:0,
    ItemCfgData:null,
    init:function()
    {
        this.dataList=new window.List();
        this.dataDictiony=new window.dictionary();
        this.dataAndPefabDic=new window.dictionary();
        this.loadPath=window.Constant.RootPath.RewardItem;
        this.ItemCfgData=window.cfg.RewardItemCfgData.GetRewardItemCfgData();
        var len=this.ItemCfgData.count;
        for(var i=0;i<len;i++)
        {
            var data=this.ItemCfgData.values[i];
            if(data==null)
            {
                console.log("未取得配置文件索引:"+i);
                return;
            }       
            this.LoadPrefab(data);     
        }

    },
    //加载预制体
    LoadPrefab:function(data)
    {
        var path=this.loadPath+data.path;
        window.ResourceManager.LoadPrefab(path,function(err, prefab)
         {
             if(err!=null)
             {
                 console.log("错误信息:"+err);
                 return;
             }
             this.dataAndPefabDic.add(data.id,prefab);
         }.bind(this)); 
    },

    initStarPool:function()
    {  
        var len=this.dataAndPefabDic.count;
        for(var k=0;k<len;k++)
        {
            var pool=new cc.NodePool();
            var key=this.dataAndPefabDic.keys[k];
            for(var i=0;i<this.initCount;i++)
            {              
                var prefab=this.dataAndPefabDic.get(key);         
                var objNode=cc.instantiate(prefab);
                //添加脚本可以根据key值进行改变,也可以在RewardHandler中判断Key编写不一样的逻辑
                objNode.addComponent("RewardHandler");
                pool.put(objNode);
            }
            this.dataDictiony.add(key,pool);
        }           
    },

    GetRewardItemFromPool:function(parent,id,pos,manager)
    {    
        this.manager=manager;       
        var nodeObj;
        var pool=this.dataDictiony.get(id);
        if(pool==null)
        {
            console.log("id:"+id+"对象池为空");
            return;
        }
        if(pool.size()>0)
        {
            nodeObj=pool.get(); 
        }     
        else
        {
            var prefab=this.dataAndPefabDic.get(id);         
            nodeObj=cc.instantiate(prefab);
            nodeObj.addComponent("RewardHandler");
        }
        nodeObj.getComponent("RewardHandler").SetData(id,manager); 
        nodeObj.setParent(parent);
        nodeObj.setPosition(pos);
        return nodeObj;
    },

    RemoveRewardItem:function(objNode)
    {
        var id=objNode.getComponent("RewardHandler").id;
        this.PutRewardItemToPool(objNode,id);
    },   
    PutRewardItemToPool:function(objNode,id)
    {    
        var pool=this.dataDictiony.get(id);
        pool.put(objNode);
    },  
    RemoveAllItem()
    {
        if(!this.manager)
        {
            return;
        }
       var SwapPosfirst1=cc.find("SwapPos",this.manager.ground1);
       var SwapPosfirst2=cc.find("SwapPos1",this.manager.ground1);

       var SwapPosSecond1=cc.find("SwapPos",this.manager.ground2);
       var SwapPosSecond2=cc.find("SwapPos1",this.manager.ground2);


       var SwapPosThird1=cc.find("SwapPos",this.manager.ground3);
       var SwapPosThird2=cc.find("SwapPos1",this.manager.ground3);

       this.RemoveChild(SwapPosfirst1);
       this.RemoveChild(SwapPosfirst2);
       this.RemoveChild(SwapPosSecond1);
       this.RemoveChild(SwapPosSecond2);
       this.RemoveChild(SwapPosThird1);
       this.RemoveChild(SwapPosThird2);
    },
    RemoveChild(parent)
    {
        var childs=parent.children;
        var len=childs.length;
        for(var i=len-1;i>=0;i--)
        {
           this.RemoveRewardItem(childs[i]);
        }
    }
}
module.exports=RewardItemData;

挂载与奖励物品上的脚本


cc.Class({
    extends: cc.Component,

     Data:null,
     gameManager:null,
     id:0,
     onLoad () {
        this.player=this.gameManager.player;
        this.scoreNode=this.gameManager.storePos;
        this.PlayerControll=this.player.getComponent("PlayerControll");

        //被吸收功能
        // this.schedule(function()
        // {
        //     if(this.PlayerControll.absorb)
        //     {
        //       // var vecByWorld=this.gameManager.node.convertToWorldSpaceAR(this.player.position);
        //        var newVec2 = this.node.parent.convertToWorldSpaceAR(this.node.position);
        //        var newPos=this.gameManager.node.convertToNodeSpaceAR(newVec2);

        //        console.log("玩家世界坐标:"+this.gameManager.player.position);
        //        console.log("奖励物质世界坐标:"+newPos);
        //        if(this.gameManager.player.position.x<newPos.x)
        //        {
        //            var dist = newPos.x-this.gameManager.player.position.x
        //            if(dist<600)
        //            {
        //                this.MoveToPlayer();  
        //                console.log("距离范围500以内");
        //            }
        //        }
            
        //     }
        // },0.5);
     },
     update(dt)
     {
       
     },
     SetData(id,gameManager)
     {
        this.gameManager=gameManager;
        var dataList=window.cfg.RewardItemCfgData.GetRewardItemCfgData();
        var data=dataList.get(id);
        this.Data=data;
        this.id=id;

     },
     MoveToStore: function () {

        var vecByThisInTheWorld=this.scoreNode.parent.convertToWorldSpaceAR(this.scoreNode.position);
        var newVec2 = this.node.parent.convertToNodeSpaceAR(vecByThisInTheWorld);

        var speed=this.PlayerControll.cameraSpeed*15+30;  //0.3s 是0.02的15倍
        var pos =newVec2.add(new cc.v2(speed,0));
        var moveUp=cc.moveTo(0.3,pos).easing(cc.easeCircleActionInOut());  
        var seq=cc.sequence(moveUp,cc.callFunc(function()
        {
            window.data.RewardItemData.RemoveRewardItem(this.node);   
            this.addSorce();   
        }.bind(this)));

        this.node.runAction(seq);
    },
    MoveToPlayer()
    {
     
        var vecByThisInTheWorld=this.scoreNode.parent.convertToWorldSpaceAR(this.player.position);
        var newVec2 = this.node.parent.convertToNodeSpaceAR(vecByThisInTheWorld);
        var pos =newVec2.add(new cc.v2(120,0));
        var moveUp=cc.moveTo(0.4,pos).easing(cc.easeCircleActionInOut());  
        var seq=cc.sequence(moveUp,cc.callFunc(function()
        {
            window.data.RewardItemData.RemoveRewardItem(this.node);   
            this.addSorce();   
          
        }.bind(this)));

        this.node.runAction(seq);
    },
    onCollisionEnter:function(other,self)
    {

        if(other.tag==1)
        {        
            this.MoveToStore();   //金币移动效果 
            window.AudioController.playSXF(window.Constant.GameClip.score);
        } 
        else if(other.tag==50)
        {
            window.data.RewardItemData.RemoveRewardItem(this.node);    
        }
    },
    ///添加分数
    addSorce:function()
    {
        
        window.data.GameRunData.SetSorce(this.Data.score);
    },
});

游戏二维码

微信平台

19.Cocos跑酷游戏——06添加奖励物品

4399平台

4399游戏链接:http://www.4399.com/flash/203652.htm