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.最后的补充
游戏免不了来一个怪物飞袭
怪物也是通过配置表来配置的,在编辑器中做好怪物的摸样,添加动画,做成一个预制体
通过配置表读取配置表中数据,游戏运行中读取数据,获取预制体,显示出来
预制体存放位置
配置文件Buttles.json
[
{
"ID": 3001,
"name": "buttle",
"path": "buttle",
"attck": 4001,
"speed": 300
}
]
配置文件解析通用模板
var ButtlesCfgData=
{
dataList:null,
filePath:null,
init:function()
{
this.dataList=new window.dictionary();
this.filePath=window.Constant.RootPath.CONFIG_ROOT_PATH+"Buttles";
this.dataDic=window.data.ButtleData.dataDiction;
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.ButtlesCfgVo();
mVoData.SetValue(mData);
this.dataList.add(mVoData.id,mVoData);
}
}).bind(this));
},
GetBattleCfgData:function()
{
return this.dataList;
}
}
module.exports=ButtlesCfgData;
配置文件的数据对象
function ButtlesCfgVo()
{
this.id;
this.name;
this.path;
this.attck;
this.speed;
}
ButtlesCfgVo.prototype.SetValue=function(mData)
{
this.id=mData.ID;
this.name=mData.name;
this.path=mData.path;
this.attck=mData.attck;
this.speed=mData.speed;
}
module.exports=ButtlesCfgVo;
游戏中的子弹数据管理与处理
var ButtleData=
{
dataDiction:null,
dataList:null, //生产的子弹集合
initCount:3,
startArry:null,
prefab:cc.Prefab,
init:function()
{
this.dataList=new window.List();
this._pool=new cc.NodePool("ButtleHandler");
this.loadPath=window.Constant.RootPath.Buttle+window.Constant.PrefabNameEnum.Buttle;
this.InitButtle();
},
//初始化对象池
InitButtle:function()
{
window.ResourceManager.LoadPrefab(this.loadPath,function(err, prefab)
{
if(err!=null)
{
console.log("错误信息:"+err);
return;
}
this.prefab=prefab;
this.initPool();
}.bind(this));
},
//初始化对象池
initPool:function()
{
for(var i=0;i<this.initCount;i++)
{
var objNode=cc.instantiate(this.prefab);
objNode.addComponent("ButtleHandler");
this._pool.put(objNode);
}
this.startArry=new Array();
},
//从对象池中获取数据
GetButtleFromPool:function(parent,type,pos,manager)
{
this.manager=manager;
let nodeObj;
if(this._pool.size()>0)
{
nodeObj=this._pool.get();
}
else
{
nodeObj=cc.instantiate(this.prefab);
nodeObj.addComponent("ButtleHandler");
}
nodeObj.getComponent("ButtleHandler").SetData(type,manager);
nodeObj.setParent(parent);
nodeObj.setPosition(pos);
return nodeObj;
},
//获取子弹数据
GetButtleData:function(type)
{
this.dataDiction=window.cfg.ButtlesCfgData.GetBattleCfgData();
var buttle=this.dataDiction.get(type);
if(buttle==null)
{
console.log("未获取到buttle数据");
return;
}
return buttle;
},
//移除子弹
RemoveButtle:function(objNode)
{
this.PutButtleToPool(objNode);
},
PutButtleToPool:function(objNode)
{
this._pool.put(objNode);
},
//移除所有子弹数据
RemoveAllItem()
{
if(!this.manager)
{
return;
}
var buttlePos=this.manager.ButtleParent;
this.RemoveChild(buttlePos);
},
RemoveChild(parent)
{
var childs=parent.children;
var len=childs.length;
for(var i=len-1;i>0;i--)
{
this.RemoveButtle(childs[i]);
}
}
}
module.exports=ButtleData;
挂载在子弹上的脚本
cc.Class({
extends: cc.Component,
Data:null,
prePos:null,
gameManager:null,
curType:null,
onLoad () {
},
update(dt)
{
this.dt=dt;
this.Move();
},
//回收 当将节点回收时会启用这个函数
unuse:function()
{
},
//复用 对象池去对象时调用这个函数
reuse:function()
{
},
// 换图片
ChangeUIData:function(name)
{
var path=window.Constant.RootPath.ATLAS_ROOT_PATH+"Buttle/"+name;
var spriteObj=this.node.getComponent(cc.Sprite);
window.ResourceManager.LoadSpriteByPath(spriteObj,path);
},
//给当前对象绑定数据
SetData(type,gameManager)
{
this.gameManager=gameManager;
this.Data=window.data.ButtleData.GetButtleData(type);
this.curType=type;
// this.ChangeUIData(this.Data.path);
/// this.accordingTypeDesineAction();
},
//碰撞处理
onCollisionEnter:function(other,self)
{
if(other.tag==1)
{
var playerControll=other.node.getComponent("PlayerControll");
if(playerControll.invincible)
{
console.log("无敌状态");
return;
}
if(playerControll.prePlayerType==window.Constant.PlayerType.SteelBall)
{
console.log("免疫伤害");
return;
}
this.PutToPool();
this.gameManager.player.getComponent("PlayerControll").PlayerDie("撞到子弹死亡发送");
}else if(other.tag==50)
{
this.PutToPool();
}
},
//移动
Move:function()
{
this.node.x-=this.Data.speed*this.dt;
this.CheckDistance();
},
//Destory
PutToPool()
{
window.data.ButtleData.RemoveButtle(this.node);
if(this.jumpAction!=null)
{
this.node.stopAction(this.jumpAction);
}
},
//检查距离
CheckDistance:function()
{
var pos=this.gameManager.barrierPos;
if(pos==null)
return;
if(pos.x<this.node.x)
return;
var distance=pos.x-this.node.x;
if(distance>5)
{
this.PutToPool();
}
},
//根据类型定义运动轨迹
accordingTypeDesineAction()
{
switch(this.curType)
{
case window.Constant.BullteType.MiniButtle:
this.jumpHeight=200;
this.jumpAction=this.UpAndDown();
this.node.runAction(this.jumpAction);
break;
}
},
//上下移动效果
UpAndDown()
{
var jumpUp = cc.moveBy(1, cc.v2(0,this.jumpHeight)).easing(cc.easeCubicActionOut());
var jumpDown = cc.moveBy(1, cc.v2(0,-this.jumpHeight)).easing(cc.easeCubicActionIn());
return cc.repeatForever(cc.sequence(jumpUp, jumpDown));
},
});
游戏二维码
微信平台
4399平台
4399游戏链接:http://www.4399.com/flash/203652.htm