Skip to content

Commit 4f950ae

Browse files
committed
Fixed a fantastic FrameData bug. Also added support to the Emitter to handle multiple image keys and/or frames.
1 parent 5d3fe89 commit 4f950ae

10 files changed

Lines changed: 579 additions & 157 deletions

File tree

Phaser.sublime-workspace

Lines changed: 384 additions & 107 deletions
Large diffs are not rendered by default.

examples/particles.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
1616

1717
var p;
18+
var p2;
1819

1920
function preload() {
2021

@@ -23,21 +24,27 @@ function preload() {
2324
game.load.image('diamond', 'assets/sprites/diamond.png');
2425
game.load.image('dude', 'assets/sprites/phaser-dude.png');
2526
game.load.image('coke', 'assets/sprites/cokecan.png');
27+
game.load.atlasJSONHash('pixies', 'assets/sprites/pixi_monsters.png', 'assets/sprites/pixi_monsters.json');
28+
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);
2629

2730
}
2831

2932
function create() {
3033

31-
p = new Phaser.Particles.Arcade.Emitter(game, 200, 100, 500);
34+
game.stage.backgroundColor = 0x337799;
35+
36+
p = game.add.emitter(200, 100, 50);
3237

3338
// p.width = 200;
3439
// p.height = 200;
3540

36-
p.makeParticles('diamond');
41+
// keys, frames, quantity, collide
42+
// p.makeParticles(['diamond', 'carrot', 'star']);
43+
p.makeParticles('balls', [0,1,2,3,4,5]);
44+
// p.makeParticles('pixies', [0,1,2,3]);
3745

3846
// Steady constant stream at 250ms delay and 10 seconds lifespan
3947
// p.start(false, 10000, 250, 100);
40-
4148
// p.start(true, 10000);
4249

4350
// explode, lifespan, frequency, quantity
@@ -51,9 +58,6 @@ function create() {
5158
}
5259

5360
function update() {
54-
55-
p.update();
56-
5761
}
5862

5963
function render() {

src/animation/FrameData.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
1010
*/
1111
Phaser.Animation.FrameData = function () {
12+
13+
this._frames = [];
14+
this._frameNames = [];
15+
1216
};
1317

1418
Phaser.Animation.FrameData.prototype = {

src/animation/Parser.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Phaser.Animation.Parser = {
2323
// How big is our image?
2424
var img = game.cache.getImage(key);
2525

26-
if (img == null) {
26+
if (img == null)
27+
{
2728
return null;
2829
}
2930

@@ -33,13 +34,15 @@ Phaser.Animation.Parser = {
3334
var column = Math.round(height / frameHeight);
3435
var total = row * column;
3536

36-
if (frameMax !== -1) {
37+
if (frameMax !== -1)
38+
{
3739
total = frameMax;
3840
}
3941

4042
// Zero or smaller than frame sizes?
41-
if (width == 0 || height == 0 || width < frameWidth || height < frameHeight || total === 0) {
42-
throw new Error("AnimationLoader.parseSpriteSheet: width/height zero or width/height < given frameWidth/frameHeight");
43+
if (width == 0 || height == 0 || width < frameWidth || height < frameHeight || total === 0)
44+
{
45+
console.warn("Phaser.Animation.Parser.spriteSheet: width/height zero or width/height < given frameWidth/frameHeight");
4346
return null;
4447
}
4548

@@ -82,9 +85,11 @@ Phaser.Animation.Parser = {
8285
JSONData: function (game, json, cacheKey) {
8386

8487
// Malformed?
85-
if (!json['frames']) {
88+
if (!json['frames'])
89+
{
90+
console.warn("Phaser.Animation.Parser.JSONData: Invalid Texture Atlas JSON given, missing 'frames' array");
8691
console.log(json);
87-
throw new Error("Phaser.AnimationLoader.parseJSONData: Invalid Texture Atlas JSON given, missing 'frames' array");
92+
return;
8893
}
8994

9095
// Let's create some frames then
@@ -143,14 +148,16 @@ Phaser.Animation.Parser = {
143148
JSONDataHash: function (game, json, cacheKey) {
144149

145150
// Malformed?
146-
if (!json['frames']) {
151+
if (!json['frames'])
152+
{
153+
console.warn("Phaser.Animation.Parser.JSONDataHash: Invalid Texture Atlas JSON given, missing 'frames' object");
147154
console.log(json);
148-
throw new Error("Phaser.AnimationLoader.parseJSONDataHash: Invalid Texture Atlas JSON given, missing 'frames' object");
155+
return;
149156
}
150157

151158
// Let's create some frames then
152159
var data = new Phaser.Animation.FrameData();
153-
160+
154161
// By this stage frames is a fully parsed array
155162
var frames = json['frames'];
156163
var newFrame;
@@ -204,8 +211,10 @@ Phaser.Animation.Parser = {
204211
XMLData: function (game, xml, cacheKey) {
205212

206213
// Malformed?
207-
if (!xml.getElementsByTagName('TextureAtlas')) {
208-
throw new Error("Phaser.AnimationLoader.parseXMLData: Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
214+
if (!xml.getElementsByTagName('TextureAtlas'))
215+
{
216+
console.warn("Phaser.Animation.Parser.XMLData: Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
217+
return;
209218
}
210219

211220
// Let's create some frames then

src/core/Game.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Phaser.Game.prototype = {
305305
this.input = new Phaser.Input(this);
306306
this.sound = new Phaser.SoundManager(this);
307307
this.physics = new Phaser.Physics.Arcade(this);
308+
this.particles = new Phaser.Particles(this);
308309
this.plugins = new Phaser.PluginManager(this, this);
309310
this.net = new Phaser.Net(this);
310311
this.debug = new Phaser.Utils.Debug(this);
@@ -391,6 +392,7 @@ Phaser.Game.prototype = {
391392
this.tweens.update();
392393
this.sound.update();
393394
this.world.update();
395+
this.particles.update();
394396
this.state.update();
395397
this.plugins.update();
396398

src/gameobjects/GameObjectFactory.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,10 @@ Phaser.GameObjectFactory.prototype = {
9191

9292
},
9393

94+
emitter: function (x, y, maxParticles) {
95+
96+
return this.game.particles.add(new Phaser.Particles.Arcade.Emitter(this.game, x, y, maxParticles));
97+
98+
},
99+
94100
};

src/gameobjects/Sprite.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ Phaser.Sprite = function (game, x, y, key, frame) {
2323
// The lifespan is decremented by game.time.elapsed each update, once it reaches zero the kill() function is called.
2424
this.lifespan = 0;
2525

26-
if (key)
26+
if (key == null || this.game.cache.checkImageKey(key) == false)
2727
{
28-
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
29-
}
30-
else
31-
{
32-
// No texture yet
33-
PIXI.Sprite.call(this);
28+
key = '__default';
3429
}
3530

31+
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
32+
3633
this.events = new Phaser.Events(this);
3734

3835
/**

src/loader/Cache.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
1111
*/
1212
Phaser.Cache = function (game) {
13+
1314
this.game = game;
15+
this._canvases = {};
16+
this._images = {};
17+
this._sounds = {};
18+
this._text = {};
19+
20+
this.addDefaultImage();
21+
1422
};
1523

1624
Phaser.Cache.prototype = {
@@ -106,6 +114,25 @@ Phaser.Cache.prototype = {
106114

107115
},
108116

117+
/**
118+
* Adds a default image to be used when a key is wrong / missing.
119+
* Is mapped to the key __default
120+
*/
121+
addDefaultImage: function () {
122+
123+
this._images['__default'] = { url: null, data: null, spriteSheet: false };
124+
this._images['__default'].frame = new Phaser.Animation.Frame(0, 0, 32, 32, '', '');
125+
126+
var base = new PIXI.BaseTexture();
127+
base.width = 32;
128+
base.height = 32;
129+
base.hasLoaded = true; // avoids a hanging event listener
130+
131+
PIXI.BaseTextureCache['__default'] = base;
132+
PIXI.TextureCache['__default'] = new PIXI.Texture(base);
133+
134+
},
135+
109136
/**
110137
* Add a new image.
111138
* @param key {string} Asset key for the image.
@@ -219,6 +246,22 @@ Phaser.Cache.prototype = {
219246
return null;
220247
},
221248

249+
/**
250+
* Checks if an image key exists.
251+
* @param key Asset key of the image you want.
252+
* @return {boolean} True if the key exists, otherwise false.
253+
*/
254+
checkImageKey: function (key) {
255+
256+
if (this._images[key])
257+
{
258+
return true;
259+
}
260+
261+
return false;
262+
263+
},
264+
222265
/**
223266
* Get image data by key.
224267
* @param key Asset key of the image you want.

src/particles/Particles.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
Phaser.Particles = {}
1+
Phaser.Particles = function (game) {
2+
3+
this.emitters = {};
4+
5+
this.ID = 0;
6+
7+
};
8+
9+
Phaser.Particles.prototype = {
10+
11+
emitters: null,
12+
13+
add: function (emitter) {
14+
15+
this.emitters[emitter.name] = emitter;
16+
17+
return emitter;
18+
19+
},
20+
21+
remove: function (emitter) {
22+
23+
delete this.emitters[emitter.name];
24+
25+
},
26+
27+
update: function () {
28+
29+
for (var key in this.emitters)
30+
{
31+
if (this.emitters[key].exists)
32+
{
33+
this.emitters[key].update();
34+
}
35+
}
36+
37+
},
38+
39+
};

0 commit comments

Comments
 (0)