1.Cocos跑酷游戏——List工具篇

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.最后的补充

CocosCrearot的开发 使用的JS,
Js语言 不像 C# java 有着封装好的数据结构 比如 List Dictionry 这些容器
如果对JS 的面向对象不太熟悉可以 参考 (https://www.cnblogs.com/pompey/p/6675559.html)

那么我们第一步 就是先 写一些 需要用到的,之后有需要 要进一步添加。

直接上源码,源码上尽可能的添加注释 ,有不清楚的可以在下方留言。

代码目录结构:

1.Cocos跑酷游戏——List工具篇

文件名 List.js

 
 //定义一个List类型 
function List()
{

     //定义 List的长度 
     this.count = 0;
     // 申明一个JS的数组,作为存储容器
     this.values = new Array();
}
// 返回List的长度
 List.prototype.length=function()
 {
     return this.count;
 }

// 检测 List 中是否存在 相应的value值 存在返回 在数组中的索引
//不存在返回 -1
 List.prototype.checkValue = function(value)
 {   
     for(var i=0;i<this.count;i++)
     {  
        var isExist = false;      
        isExist = this.values[i]==value;
         if(isExist)
         {
             return i;            
         }
     }
     return -1;
 },
 //检测 List 中是否存在 value 值,存在返回 true 不存在返回 false
 List.prototype.contains = function(value)
 {   
     for(var i=0;i<this.count;i++)
     {  
        var isExist = false;      
        isExist = this.values[i]==value;
         if(isExist)
         {
             return true;            
         }
     }
     return false;
 },
 //往 List 中 添加 值value List的长度 +1
 //添加值的方法 为 原始JS 数组的操作方式
 List.prototype.add = function(value)
 {
    this.values.push(value);
    this.count = this.count + 1;
 },
 //根据 索引 移除List中 的value 值 List count -1; 
 List.prototype.removeByIndex = function(index)
 {
    this.values.splice(index,1);
    this.count = this.count-1;
 },
//根据 值 移除 list 中 存在的 相应 值  List count-1
 List.prototype.remove = function(value)
 {
    var index = this.checkValue(value);
    if(index >= 0)
    {
        this.values.splice(index,1);
        this.count = this.count-1;
    }
 },
// 根据 索引 获得 List 中 存在的值
 List.prototype.get = function(index)
 {
    if(index>=this.count)
    {
        console.log("Array crossing");
        return;
    }
    return this.values[index];
 },
// 清除 List 中 保存的所有值 ,List的 Count-1
 List.prototype.clear = function()
 {
    this.values.splice(0,this.count);
    this.count = 0;
 },

 module.exports = List;