Skip to content

Commit 275fa46

Browse files
committed
Added Text.destroy and BitmapText.destroy.
1 parent 4b2ac6c commit 275fa46

6 files changed

Lines changed: 92 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Version 1.0.7 (in progress in the dev branch)
123123
* Fixed an issue where creating an animation with just one frame with an index of zero would cause a UUID error (thanks SYNYST3R1)
124124
* Fixed Rectangle.union (thanks andron77)
125125
* Debug.renderSpriteBody updated to use a the new Sprite.Body.screenX/Y properties.
126+
* Added Text.destroy() and BitmapText.destroy(), also updated Group.remove to make it more bullet-proof when an element doesn't have any events.
127+
126128

127129

128130

examples/text/hello arial.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
<script type="text/javascript">
77

8-
9-
108
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { create: create });
119

1210
function create() {
@@ -19,8 +17,6 @@ function create() {
1917

2018
}
2119

22-
23-
2420
</script>
2521

2622
<?php

examples/text/remove text.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
$title = "Removing Text";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { create: create });
9+
10+
var text;
11+
12+
function create() {
13+
14+
text = game.add.text(game.world.centerX, game.world.centerY, "- phaser -\nclick to remove", { font: "65px Arial", fill: "#ff0044", align: "center" });
15+
text.anchor.setTo(0.5, 0.5);
16+
17+
game.input.onDown.addOnce(removeText, this);
18+
19+
}
20+
21+
function removeText() {
22+
23+
// game.world.remove(text);
24+
text.destroy();
25+
26+
}
27+
28+
</script>
29+
30+
<?php
31+
require('../foot.php');
32+
?>

src/core/Group.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,13 @@ Phaser.Group.prototype = {
936936
*/
937937
remove: function (child) {
938938

939-
child.events.onRemovedFromGroup.dispatch(child, this);
939+
if (child.events)
940+
{
941+
child.events.onRemovedFromGroup.dispatch(child, this);
942+
}
943+
940944
this._container.removeChild(child);
945+
941946
child.group = null;
942947

943948
},

src/gameobjects/BitmapText.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,32 @@ Phaser.BitmapText.prototype.update = function() {
145145

146146
}
147147

148+
/**
149+
* @method Phaser.Text.prototype.destroy
150+
*/
151+
Phaser.BitmapText.prototype.destroy = function() {
152+
153+
if (this.group)
154+
{
155+
this.group.remove(this);
156+
}
157+
158+
if (this.canvas.parentNode)
159+
{
160+
this.canvas.parentNode.removeChild(this.canvas);
161+
}
162+
else
163+
{
164+
this.canvas = null;
165+
this.context = null;
166+
}
167+
168+
this.exists = false;
169+
170+
this.group = null;
171+
172+
}
173+
148174
/**
149175
* Get
150176
* @returns {Description}

src/gameobjects/Text.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ Phaser.Text = function (game, x, y, text, style) {
114114
Phaser.Text.prototype = Object.create(PIXI.Text.prototype);
115115
Phaser.Text.prototype.constructor = Phaser.Text;
116116

117-
118117
/**
119118
* Automatically called by World.update.
120119
* @method Phaser.Text.prototype.update
@@ -140,6 +139,32 @@ Phaser.Text.prototype.update = function() {
140139

141140
}
142141

142+
/**
143+
* @method Phaser.Text.prototype.destroy
144+
*/
145+
Phaser.Text.prototype.destroy = function() {
146+
147+
if (this.group)
148+
{
149+
this.group.remove(this);
150+
}
151+
152+
if (this.canvas.parentNode)
153+
{
154+
this.canvas.parentNode.removeChild(this.canvas);
155+
}
156+
else
157+
{
158+
this.canvas = null;
159+
this.context = null;
160+
}
161+
162+
this.exists = false;
163+
164+
this.group = null;
165+
166+
}
167+
143168
/**
144169
* Get
145170
* @returns {Description}

0 commit comments

Comments
 (0)