Skip to content

Commit 8a90a87

Browse files
committed
Text converted and a couple of examples created. Using new extend system, so much smaller classes now.
1 parent 5acb4e9 commit 8a90a87

9 files changed

Lines changed: 124 additions & 34 deletions

File tree

examples/js.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<script src="../src/gameobjects/GameObjectFactory.js"></script>
6666
<script src="../src/gameobjects/Sprite.js"></script>
6767
<script src="../src/gameobjects/TileSprite.js"></script>
68+
<script src="../src/gameobjects/Text.js"></script>
6869

6970
<script src="../src/system/Canvas.js"></script>
7071
<script src="../src/system/Device.js"></script>

examples/text1.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { create: create });
16+
17+
function create() {
18+
19+
var text = "- phaser -\nwith a sprinkle of\npixi dust";
20+
var style = { font: "65px Arial", fill: "#ff0044", align: "center" };
21+
22+
game.add.text(game.world.centerX, game.world.centerY, text, style).anchor.setTo(0.5, 0.5);
23+
24+
}
25+
26+
})();
27+
28+
</script>
29+
30+
</body>
31+
</html>

examples/text2.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { create: create, update: update, render: render });
16+
var s;
17+
18+
function create() {
19+
20+
var text = "--- phaser ---\nwith a sprinkle of\npixi dust";
21+
var style = { font: "bold 40pt Arial", fill: "#ffffff", align: "center", stroke: "#258acc", strokeThickness: 8 };
22+
23+
s = game.add.text(game.world.centerX, game.world.centerY, text, style);
24+
s.anchor.setTo(0.5, 0.5);
25+
26+
}
27+
28+
function update() {
29+
s.angle += 1;
30+
}
31+
32+
function render() {
33+
game.debug.renderSpriteCorners(s, true, true);
34+
}
35+
36+
})();
37+
38+
</script>
39+
40+
</body>
41+
</html>

src/gameobjects/GameObjectFactory.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ Phaser.GameObjectFactory.prototype = {
5050

5151
},
5252

53+
text: function (x, y, text, style) {
54+
55+
return this.world.add(new Phaser.Text(this.game, x, y, text, style));
56+
57+
},
5358

5459

5560
};

src/gameobjects/Sprite.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ Phaser.Sprite = function (game, x, y, key, frame) {
1717

1818
this.name = '';
1919

20+
if (key)
21+
{
22+
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
23+
}
24+
else
25+
{
26+
// No texture yet
27+
console.log('no texture yet');
28+
PIXI.Sprite.call(this);
29+
}
30+
2031
// this.events = new Phaser.Components.Events(this);
2132

2233
/**
@@ -25,7 +36,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
2536
*/
2637
this.animations = new Phaser.AnimationManager(this);
2738

28-
PIXI.DisplayObjectContainer.call(this);
39+
// PIXI.DisplayObjectContainer.call(this);
2940

3041
/**
3142
* The anchor sets the origin point of the texture.
@@ -44,7 +55,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
4455
* @property texture
4556
* @type Texture
4657
*/
47-
this.texture = PIXI.TextureCache[key];
58+
// this.texture = PIXI.TextureCache[key];
4859

4960
if (this.game.cache.isSpriteSheet(key))
5061
{
@@ -63,21 +74,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
6374
}
6475
}
6576

66-
/**
67-
* The blend mode of sprite.
68-
* currently supports PIXI.blendModes.NORMAL and PIXI.blendModes.SCREEN
69-
*
70-
* @property blendMode
71-
* @type Number
72-
*/
73-
// this.blendMode = PIXI.blendModes.NORMAL;
74-
7577
this.x = x;
7678
this.y = y;
7779

78-
// this.updateFrame = true;
79-
// this.renderable = true;
80-
8180
this.position.x = x;
8281
this.position.y = y;
8382

@@ -143,6 +142,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
143142

144143
};
145144

145+
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly)
146146
Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
147147
Phaser.Sprite.prototype.constructor = Phaser.Sprite;
148148

src/gameobjects/Text.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Phaser.Text = function (game, x, y, text, style) {
2+
3+
x = x || 0;
4+
y = y || 0;
5+
text = text || '';
6+
style = style || '';
7+
8+
this.canvas = document.createElement("canvas");
9+
this.context = this.canvas.getContext("2d");
10+
11+
var canvasID = game.rnd.uuid();
12+
13+
PIXI.TextureCache[canvasID] = new PIXI.Texture(new PIXI.BaseTexture(this.canvas));
14+
15+
Phaser.Sprite.call(this, game, x, y, canvasID);
16+
17+
this.setText(text);
18+
this.setStyle(style);
19+
20+
this.updateText();
21+
this.dirty = false;
22+
23+
};
24+
25+
Phaser.Text.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Text.prototype);
26+
Phaser.Text.prototype.constructor = Phaser.Text;
27+
28+
// Add our own custom methods

src/gameobjects/TileSprite.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,29 @@ Phaser.TileSprite = function (game, x, y, width, height, key, frame) {
88
frame = frame || null;
99

1010
Phaser.Sprite.call(this, game, x, y, key, frame);
11-
PIXI.TilingSprite.call(this);
1211

1312
this.texture = PIXI.TextureCache[key];
1413

15-
/**
16-
* The width of the tiling sprite
17-
*
18-
* @property width
19-
* @type Number
20-
*/
21-
this.width = width;
22-
23-
/**
24-
* The height of the tiling sprite
25-
*
26-
* @property height
27-
* @type Number
28-
*/
29-
this.height = height;
14+
PIXI.TilingSprite.call(this, this.texture, width, height);
3015

3116
/**
3217
* The scaling of the image that is being tiled
3318
*
3419
* @property tileScale
3520
* @type Point
3621
*/
37-
this.tileScale = new Phaser.Point(1,1);
22+
this.tileScale = new Phaser.Point(1, 1);
3823

3924
/**
4025
* The offset position of the image that is being tiled
4126
*
4227
* @property tilePosition
4328
* @type Point
4429
*/
45-
this.tilePosition = new Phaser.Point(0,0);
30+
this.tilePosition = new Phaser.Point(0, 0);
4631

4732
};
4833

49-
// Extend Phaser.Sprite and PIXI.TilingSprite
5034
Phaser.TileSprite.prototype = Phaser.Utils.extend(true, PIXI.TilingSprite.prototype, Phaser.Sprite.prototype);
5135
Phaser.TileSprite.prototype.constructor = Phaser.TileSprite;
5236

src/pixi/renderers/canvas/CanvasRenderer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject)
163163

164164
if(displayObject instanceof PIXI.Sprite)
165165
{
166-
167166
var frame = displayObject.texture.frame;
168167

169168
if(frame)

src/utils/Debug.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Phaser.Utils.Debug.prototype = {
146146

147147
if (showText)
148148
{
149+
this.currentColor = color;
149150
this.line('x: ' + Math.floor(sprite.topLeft.x) + ' y: ' + Math.floor(sprite.topLeft.y), sprite.topLeft.x, sprite.topLeft.y);
150151
this.line('x: ' + Math.floor(sprite.topRight.x) + ' y: ' + Math.floor(sprite.topRight.y), sprite.topRight.x, sprite.topRight.y);
151152
this.line('x: ' + Math.floor(sprite.bottomLeft.x) + ' y: ' + Math.floor(sprite.bottomLeft.y), sprite.bottomLeft.x, sprite.bottomLeft.y);

0 commit comments

Comments
 (0)