版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27032631/article/details/87940046
CocosCrearot的开发 使用的JS,
Js语言 不像 C# java 有着封装好的数据结构 比如 List Dictionry 这些容器
如果对JS 的面向对象不太熟悉可以 参考 (https://www.cnblogs.com/pompey/p/6675559.html)
那么我们第一步 就是先 写一些 需要用到的,之后有需要 要进一步添加。
直接上源码,源码上尽可能的添加注释 ,有不清楚的可以在下方留言。
文件名 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;