Commit d8bc031b authored by rockyl's avatar rockyl

增加makeRandom

removeChild和removeChildAt返回被删除实体
parent d4bbf423
......@@ -230,7 +230,7 @@ export class Entity extends HashObject {
removeChild(child: Entity) {
const index = this.getChildIndex(child);
if (index >= 0) {
this.removeChildAt(index);
return this.removeChildAt(index);
}
}
......@@ -240,9 +240,13 @@ export class Entity extends HashObject {
*/
removeChildAt(index: number) {
const child = this._children[index];
this._onChildRemoved(child);
this._children.splice(index, 1);
if(child){
this._onChildRemoved(child);
this._children.splice(index, 1);
return child;
}
}
/**
......
......@@ -3,12 +3,12 @@
*
* 对象池
*/
let all = {};
function getGroup(name: string){
function getGroup(name: string) {
let group = all[name];
if(!group){
if (!group) {
throw new Error('group ' + name + ' not registered.');
}
......@@ -21,7 +21,7 @@ function getGroup(name: string){
* @param newFunc 实例化方法
* @param initFunc 初始化方法
*/
export function register(name: string, newFunc: Function, initFunc: Function){
export function register(name: string, newFunc: () => any, initFunc: (instance: any, ...params) => void) {
all[name] = {name, newFunc, initFunc, pool: []};
}
......@@ -30,15 +30,15 @@ export function register(name: string, newFunc: Function, initFunc: Function){
* @param name 池名称
* @param params 参数
*/
export function get(name: string, ...params){
export function get(name: string, ...params) {
let group = getGroup(name);
let {newFunc, initFunc, pool} = group;
let instance;
if(pool.length == 0){
if (pool.length == 0) {
instance = newFunc();
}else{
} else {
instance = pool.pop();
}
......@@ -52,7 +52,7 @@ export function get(name: string, ...params){
* @param name 池名称
* @param instance 实例
*/
export function recycle(name: string, instance){
export function recycle(name: string, instance) {
let group = getGroup(name);
group.pool.push(instance);
......
......@@ -14,4 +14,9 @@ export {default as LocalStorage} from './LocalStorage'
export {TextStyle} from './TextStyle'
export {default as EventEmitter} from './EventEmitter';
export {default as dataCenter} from './DataCenter';
\ No newline at end of file
export {default as dataCenter} from './DataCenter';
import * as ObjectPool from './ObjectPool'
export {
ObjectPool
};
\ No newline at end of file
......@@ -61,6 +61,15 @@ export function makeRandomInt(max: number, min: number = 0): number {
return Math.floor(Math.random() * (max - min)) + min;
}
/**
* 随机生成一个数
* @param max
* @param min
*/
export function makeRandom(max: number, min: number = 0): number {
return Math.random() * (max - min) + min;
}
/**
* 打乱一个数组
* @param arr
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment