From 8a90a87492eaebaca39c5c5217172f3a8a06700c Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 3 Sep 2013 06:02:47 +0100 Subject: [PATCH] Text converted and a couple of examples created. Using new extend system, so much smaller classes now. --- examples/js.php | 1 + examples/text1.php | 31 ++++++++++++++++ examples/text2.php | 41 +++++++++++++++++++++ src/gameobjects/GameObjectFactory.js | 5 +++ src/gameobjects/Sprite.js | 28 +++++++------- src/gameobjects/Text.js | 28 ++++++++++++++ src/gameobjects/TileSprite.js | 22 ++--------- src/pixi/renderers/canvas/CanvasRenderer.js | 1 - src/utils/Debug.js | 1 + 9 files changed, 124 insertions(+), 34 deletions(-) create mode 100644 examples/text1.php create mode 100644 examples/text2.php create mode 100644 src/gameobjects/Text.js diff --git a/examples/js.php b/examples/js.php index 3a02be59..5804575e 100644 --- a/examples/js.php +++ b/examples/js.php @@ -65,6 +65,7 @@ + diff --git a/examples/text1.php b/examples/text1.php new file mode 100644 index 00000000..748f306b --- /dev/null +++ b/examples/text1.php @@ -0,0 +1,31 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/text2.php b/examples/text2.php new file mode 100644 index 00000000..64226d1c --- /dev/null +++ b/examples/text2.php @@ -0,0 +1,41 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/gameobjects/GameObjectFactory.js b/src/gameobjects/GameObjectFactory.js index e29b484c..63ad014b 100644 --- a/src/gameobjects/GameObjectFactory.js +++ b/src/gameobjects/GameObjectFactory.js @@ -50,6 +50,11 @@ Phaser.GameObjectFactory.prototype = { }, + text: function (x, y, text, style) { + + return this.world.add(new Phaser.Text(this.game, x, y, text, style)); + + }, }; \ No newline at end of file diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 31863238..8591500c 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -17,6 +17,17 @@ Phaser.Sprite = function (game, x, y, key, frame) { this.name = ''; + if (key) + { + PIXI.Sprite.call(this, PIXI.TextureCache[key]); + } + else + { + // No texture yet + console.log('no texture yet'); + PIXI.Sprite.call(this); + } + // this.events = new Phaser.Components.Events(this); /** @@ -25,7 +36,7 @@ Phaser.Sprite = function (game, x, y, key, frame) { */ this.animations = new Phaser.AnimationManager(this); - PIXI.DisplayObjectContainer.call(this); + // PIXI.DisplayObjectContainer.call(this); /** * The anchor sets the origin point of the texture. @@ -44,7 +55,7 @@ Phaser.Sprite = function (game, x, y, key, frame) { * @property texture * @type Texture */ - this.texture = PIXI.TextureCache[key]; + // this.texture = PIXI.TextureCache[key]; if (this.game.cache.isSpriteSheet(key)) { @@ -63,21 +74,9 @@ Phaser.Sprite = function (game, x, y, key, frame) { } } - /** - * The blend mode of sprite. - * currently supports PIXI.blendModes.NORMAL and PIXI.blendModes.SCREEN - * - * @property blendMode - * @type Number - */ - // this.blendMode = PIXI.blendModes.NORMAL; - this.x = x; this.y = y; - // this.updateFrame = true; - // this.renderable = true; - this.position.x = x; this.position.y = y; @@ -143,6 +142,7 @@ Phaser.Sprite = function (game, x, y, key, frame) { }; +// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly) Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype); Phaser.Sprite.prototype.constructor = Phaser.Sprite; diff --git a/src/gameobjects/Text.js b/src/gameobjects/Text.js new file mode 100644 index 00000000..b86130a7 --- /dev/null +++ b/src/gameobjects/Text.js @@ -0,0 +1,28 @@ +Phaser.Text = function (game, x, y, text, style) { + + x = x || 0; + y = y || 0; + text = text || ''; + style = style || ''; + + this.canvas = document.createElement("canvas"); + this.context = this.canvas.getContext("2d"); + + var canvasID = game.rnd.uuid(); + + PIXI.TextureCache[canvasID] = new PIXI.Texture(new PIXI.BaseTexture(this.canvas)); + + Phaser.Sprite.call(this, game, x, y, canvasID); + + this.setText(text); + this.setStyle(style); + + this.updateText(); + this.dirty = false; + +}; + +Phaser.Text.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Text.prototype); +Phaser.Text.prototype.constructor = Phaser.Text; + +// Add our own custom methods diff --git a/src/gameobjects/TileSprite.js b/src/gameobjects/TileSprite.js index b3c70e66..46bcf36b 100644 --- a/src/gameobjects/TileSprite.js +++ b/src/gameobjects/TileSprite.js @@ -8,25 +8,10 @@ Phaser.TileSprite = function (game, x, y, width, height, key, frame) { frame = frame || null; Phaser.Sprite.call(this, game, x, y, key, frame); - PIXI.TilingSprite.call(this); this.texture = PIXI.TextureCache[key]; - /** - * The width of the tiling sprite - * - * @property width - * @type Number - */ - this.width = width; - - /** - * The height of the tiling sprite - * - * @property height - * @type Number - */ - this.height = height; + PIXI.TilingSprite.call(this, this.texture, width, height); /** * The scaling of the image that is being tiled @@ -34,7 +19,7 @@ Phaser.TileSprite = function (game, x, y, width, height, key, frame) { * @property tileScale * @type Point */ - this.tileScale = new Phaser.Point(1,1); + this.tileScale = new Phaser.Point(1, 1); /** * The offset position of the image that is being tiled @@ -42,11 +27,10 @@ Phaser.TileSprite = function (game, x, y, width, height, key, frame) { * @property tilePosition * @type Point */ - this.tilePosition = new Phaser.Point(0,0); + this.tilePosition = new Phaser.Point(0, 0); }; -// Extend Phaser.Sprite and PIXI.TilingSprite Phaser.TileSprite.prototype = Phaser.Utils.extend(true, PIXI.TilingSprite.prototype, Phaser.Sprite.prototype); Phaser.TileSprite.prototype.constructor = Phaser.TileSprite; diff --git a/src/pixi/renderers/canvas/CanvasRenderer.js b/src/pixi/renderers/canvas/CanvasRenderer.js index f0b9b918..45cc80df 100644 --- a/src/pixi/renderers/canvas/CanvasRenderer.js +++ b/src/pixi/renderers/canvas/CanvasRenderer.js @@ -163,7 +163,6 @@ PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject) if(displayObject instanceof PIXI.Sprite) { - var frame = displayObject.texture.frame; if(frame) diff --git a/src/utils/Debug.js b/src/utils/Debug.js index 9dc49737..e279b0fc 100644 --- a/src/utils/Debug.js +++ b/src/utils/Debug.js @@ -146,6 +146,7 @@ Phaser.Utils.Debug.prototype = { if (showText) { + this.currentColor = color; this.line('x: ' + Math.floor(sprite.topLeft.x) + ' y: ' + Math.floor(sprite.topLeft.y), sprite.topLeft.x, sprite.topLeft.y); this.line('x: ' + Math.floor(sprite.topRight.x) + ' y: ' + Math.floor(sprite.topRight.y), sprite.topRight.x, sprite.topRight.y); this.line('x: ' + Math.floor(sprite.bottomLeft.x) + ' y: ' + Math.floor(sprite.bottomLeft.y), sprite.bottomLeft.x, sprite.bottomLeft.y);