前端之家收集整理的这篇文章主要介绍了
Cocos2d-JS 实现Map Vector,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/**
* Map
* Created by Lovell on 16/6/20.
*/
pg.Map = cc.Class.extend({
_elements : null
});
/**
* 创建Map
* @H_301_32@@returns {*}
*/
pg.Map.create = function ()
{
var res = new pg.Map();
res.clear();
return res;
};
/**
* Map元素的长度
* @H_301_32@@returns {*}
*/
pg.Map.prototype.size = function () {
return this._elements.length;
};
/**
* 判断Map是否为空
* @H_301_32@@returns {boolean}
*/
pg.Map.prototype.isEmpty = function () {
return (this._elements.length < 1);
};
pg.Map.prototype.clear = function () {
this._elements = [];
};
/**
* 向Map中增加元素
* @H_301_32@@param key
* @H_301_32@@param value
*/
pg.Map.prototype.put = function (key,value) {
if (this.containKey(key)) {
this.remove(key);
}
this._elements.push({
key: key, value: value
});
};
/**
* 向Map中插入元素
* @H_301_32@@param index
* @H_301_32@@param key
* @H_301_32@@param value
*/
pg.Map.prototype.insert = function (index,key,value) {
if (this.containKey(key)) {
this.remove(key);
}
this._elements.splice(index,0,{
key: key, value: value
});
};
/**
* 删除指定Key的元素
* @H_301_32@@param key
* @H_301_32@@returns {boolean}
*/
pg.Map.prototype.remove = function (key) {
var ret = false;
try {
for (var i=0; i<this._elements.length; i++) {
if (this._elements[i].key == key)
{
this._elements.splice(i,1);
return true;
}
}
} catch (e) {
ret = false;
}
return ret;
};
/**
* 获取指定Key的元素值Value,失败返回NULL
* @H_301_32@@param key
* @H_301_32@@returns {*}
*/
pg.Map.prototype.get = function (key) {
try {
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i].key == key) {
return this._elements[i].value;
}
}
} catch (e) {
return null;
}
};
/**
* 设置指定Key的元素值Value,失败返回FALSE
* @H_301_32@@param key
* @H_301_32@@param value
* @H_301_32@@returns {boolean}
*/
pg.Map.prototype.set = function (key,value) {
try {
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i].key == key) {
this._elements[i].value = value;
return true;
}
}
} catch (e) {
return false;
}
};
/**
* 通过Index获取对应的Value
* @H_301_32@@param index
* @H_301_32@@returns {*}
*/
pg.Map.prototype.getValueByIndex = function (index) {
return this._elements[index].value;
};
/**
* 通过Key获取对应的Index
* @H_301_32@@param key
* @H_301_32@@returns {number}
*/
pg.Map.prototype.getIndexByKey = function (key) {
try {
for (i = 0; i < this._elements.length; i++) {
if (this._elements[i].key == key) {
return i;
}
}
} catch (e) {
return -1;
}
};
/**
* 通过Index获得Key
* @H_301_32@@param index
* @H_301_32@@returns {*}
*/
pg.Map.prototype.getKeyByIndex = function (index) {
return this._elements[index].key;
};
pg.Map.prototype.removeByIndex = function (index) {
this.remove(this._elements[index].key);
};
/**
* 获取指定索引的元素
* @H_301_32@@param index
* @H_301_32@@returns {*}
*/
pg.Map.prototype.element = function (index) {
if (index < 0 || index >= this._elements.length) {
return null;
}
return this._elements[index];
};
/**
* 判断Map中是否含有指定KEY的元素
* @H_301_32@@param key
* @H_301_32@@returns {boolean}
*/
pg.Map.prototype.containKey = function (key) {
var ret = false;
try {
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i].key == key) {
ret = true;
}
}
} catch (e) {
ret = false;
}
return ret;
};
/**
* 判断Map中是否含有指定VALUE的元素
* @H_301_32@@param value
* @H_301_32@@returns {boolean}
*/
pg.Map.prototype.containValue = function (value) {
var ret = false;
try {
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i].value == value) {
ret = true;
}
}
} catch (e) {
ret = false;
}
return ret;
};
/**
* 获取Map中所有VALUE的数组
* @H_301_32@@returns {Array}
*/
pg.Map.prototype.values = function () {
var arr = [];
for (var i = 0; i < this._elements.length; i++) {
arr.push(this._elements[i].value);
}
return arr;
};
/**
* 获取Map中所有KEY的数组
* @H_301_32@@returns {Array}
*/
pg.Map.prototype.keys = function () {
var arr = [];
for (var i = 0; i < this._elements.length; i++) {
arr.push(this._elements[i].key);
}
return arr;
};
/**************************************************************************************************************************************************************************************
* Created by Lovell on 8/1/16.
*/
pg.Vector = cc.Class.extend({
_array: null
});
pg.Vector.create = function () {
var res = new pg.Vector();
if (res.init()) {
return res;
}
res = null;
return null;
};
pg.Vector.prototype.init = function () {
this._array = [];
return true;
};
pg.Vector.prototype.push = function (obj) {
this._array.push(obj);
};
pg.Vector.prototype.at = function (index) {
if (index < 0 || index >= this._array.length) {
return null;
}
return this._array[index];
};
pg.Vector.prototype.erase = function (obj) {
for (var i = 0; i < this._array.length; i++) {
if (this._array[i] == obj) {
this._array.splice(i,1);
break;
}
}
};
pg.Vector.prototype.eraseAt = function (index) {
this._array.splice(index,1);
};
pg.Vector.prototype.contains = function (obj) {
for (var i = 0; i < this._array.length; i++) {
if (this._array[i] == obj) {
return true;
}
}
return false;
};
pg.Vector.prototype.size = function () {
return this._array.length;
};
pg.Vector.prototype.insert = function (index,obj) {
this._array.splice(index,obj);
};
pg.Vector.prototype.clear = function () {
this._array.length = 0;
};
原文链接:https://www.f2er.com/cocos2dx/338161.html