From e66609d953fcbe922d6fa34e856e438bb7014594 Mon Sep 17 00:00:00 2001 From: Oleg Kuzyk Date: Mon, 3 Feb 2014 15:04:02 +0200 Subject: [PATCH] Reusing children and use pool object. When need update BitmapText very often it is not the best way to create new Sprites and remove old. So better way is reusing it and use pool object. --- src/pixi/text/BitmapText.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/pixi/text/BitmapText.js b/src/pixi/text/BitmapText.js index b617c7f..3029ce5 100644 --- a/src/pixi/text/BitmapText.js +++ b/src/pixi/text/BitmapText.js @@ -20,6 +20,8 @@ PIXI.BitmapText = function(text, style) { PIXI.DisplayObjectContainer.call(this); + this._pool = []; + this.setText(text); this.setStyle(style); this.updateText(); @@ -125,13 +127,28 @@ PIXI.BitmapText.prototype.updateText = function() lineAlignOffsets.push(alignOffset); } - for(i = 0; i < chars.length; i++) + var lenChildren = this.children.length; + var lenChars = chars.length; + for(i = 0; i < lenChars; i++) { - var c = new PIXI.Sprite(chars[i].texture); //PIXI.Sprite.fromFrame(chars[i].charCode); + var c = i < lenChildren ? this.children[i] : this._pool.pop(); // get old child if have. if not - take from pool. + + if (c) c.setTexture(chars[i].texture); // check if got one before. + else c = new PIXI.Sprite(chars[i].texture); // if no create new one. + c.position.x = (chars[i].position.x + lineAlignOffsets[chars[i].line]) * scale; c.position.y = chars[i].position.y * scale; c.scale.x = c.scale.y = scale; - this.addChild(c); + if (!c.parent) this.addChild(c); + } + + // remove unnecessary children. + // and put their into the pool. + while(this.children.length > lenChars) + { + var child = this.getChildAt(this.children.length - 1); + this._pool.push(child); + this.removeChild(child); } this.width = maxLineWidth * scale; @@ -148,12 +165,7 @@ PIXI.BitmapText.prototype.updateTransform = function() { if(this.dirty) { - while(this.children.length > 0) - { - this.removeChild(this.getChildAt(0)); - } this.updateText(); - this.dirty = false; }