Commit d8bc031b authored by rockyl's avatar rockyl

增加makeRandom

removeChild和removeChildAt返回被删除实体
parent d4bbf423
...@@ -230,7 +230,7 @@ export class Entity extends HashObject { ...@@ -230,7 +230,7 @@ export class Entity extends HashObject {
removeChild(child: Entity) { removeChild(child: Entity) {
const index = this.getChildIndex(child); const index = this.getChildIndex(child);
if (index >= 0) { if (index >= 0) {
this.removeChildAt(index); return this.removeChildAt(index);
} }
} }
...@@ -240,9 +240,13 @@ export class Entity extends HashObject { ...@@ -240,9 +240,13 @@ export class Entity extends HashObject {
*/ */
removeChildAt(index: number) { removeChildAt(index: number) {
const child = this._children[index]; 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 @@ ...@@ -3,12 +3,12 @@
* *
* 对象池 * 对象池
*/ */
let all = {}; let all = {};
function getGroup(name: string){ function getGroup(name: string) {
let group = all[name]; let group = all[name];
if(!group){ if (!group) {
throw new Error('group ' + name + ' not registered.'); throw new Error('group ' + name + ' not registered.');
} }
...@@ -21,7 +21,7 @@ function getGroup(name: string){ ...@@ -21,7 +21,7 @@ function getGroup(name: string){
* @param newFunc 实例化方法 * @param newFunc 实例化方法
* @param initFunc 初始化方法 * @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: []}; all[name] = {name, newFunc, initFunc, pool: []};
} }
...@@ -30,15 +30,15 @@ export function register(name: string, newFunc: Function, initFunc: Function){ ...@@ -30,15 +30,15 @@ export function register(name: string, newFunc: Function, initFunc: Function){
* @param name 池名称 * @param name 池名称
* @param params 参数 * @param params 参数
*/ */
export function get(name: string, ...params){ export function get(name: string, ...params) {
let group = getGroup(name); let group = getGroup(name);
let {newFunc, initFunc, pool} = group; let {newFunc, initFunc, pool} = group;
let instance; let instance;
if(pool.length == 0){ if (pool.length == 0) {
instance = newFunc(); instance = newFunc();
}else{ } else {
instance = pool.pop(); instance = pool.pop();
} }
...@@ -52,7 +52,7 @@ export function get(name: string, ...params){ ...@@ -52,7 +52,7 @@ export function get(name: string, ...params){
* @param name 池名称 * @param name 池名称
* @param instance 实例 * @param instance 实例
*/ */
export function recycle(name: string, instance){ export function recycle(name: string, instance) {
let group = getGroup(name); let group = getGroup(name);
group.pool.push(instance); group.pool.push(instance);
......
...@@ -14,4 +14,9 @@ export {default as LocalStorage} from './LocalStorage' ...@@ -14,4 +14,9 @@ export {default as LocalStorage} from './LocalStorage'
export {TextStyle} from './TextStyle' export {TextStyle} from './TextStyle'
export {default as EventEmitter} from './EventEmitter'; export {default as EventEmitter} from './EventEmitter';
export {default as dataCenter} from './DataCenter'; export {default as dataCenter} from './DataCenter';
\ No newline at end of file
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 { ...@@ -61,6 +61,15 @@ export function makeRandomInt(max: number, min: number = 0): number {
return Math.floor(Math.random() * (max - min)) + min; 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 * @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