Skip to content

Commit 7f3b542

Browse files
committed
Removed the notion of Sprite Sheets vs single frame images
Instead of making a distinction between single frame images and Sprite Sheets, I added a simple FrameData with one Frame to every image that gets loaded. This way, a lot of the engine code can be simplified. For instance, you can play animations on any Sprite without having to check the “frame count” of its image anymore. The engine doesn’t have to check for non-existing .frameData anymore. An animation can keep on playing when an image happens to be set with 1 frame, continuing its animation when after that, another image is set with multiple frames. Think of it this way: an image with one frame is an animation of just one frame.
1 parent fc047c5 commit 7f3b542

2 files changed

Lines changed: 29 additions & 39 deletions

File tree

src/gameobjects/Image.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -251,32 +251,21 @@ Phaser.Image.prototype.loadTexture = function (key, frame) {
251251
return;
252252
}
253253

254-
if (this.game.cache.isSpriteSheet(key))
255-
{
256-
this.key = key;
254+
this.key = key;
257255

258-
var frameData = this.game.cache.getFrameData(key);
256+
var frameData = this.game.cache.getFrameData(key);
259257

260-
if (typeof frame === 'string')
261-
{
262-
this._frame = 0;
263-
this._frameName = frame;
264-
this.setTexture(PIXI.TextureCache[frameData.getFrameByName(frame).uuid]);
265-
return;
266-
}
267-
else
268-
{
269-
this._frame = frame;
270-
this._frameName = '';
271-
this.setTexture(PIXI.TextureCache[frameData.getFrame(frame).uuid]);
272-
return;
273-
}
258+
if (typeof frame === 'string')
259+
{
260+
this._frame = 0;
261+
this._frameName = frame;
262+
this.setTexture(PIXI.TextureCache[frameData.getFrameByName(frame).uuid]);
274263
}
275264
else
276265
{
277-
this.key = key;
278-
this.setTexture(PIXI.TextureCache[key]);
279-
return;
266+
this._frame = frame;
267+
this._frameName = '';
268+
this.setTexture(PIXI.TextureCache[frameData.getFrame(frame).uuid]);
280269
}
281270
}
282271

@@ -613,7 +602,7 @@ Object.defineProperty(Phaser.Image.prototype, "frame", {
613602

614603
set: function(value) {
615604

616-
if (value !== this.frame && this.game.cache.isSpriteSheet(this.key))
605+
if (value !== this.frame)
617606
{
618607
var frameData = this.game.cache.getFrameData(this.key);
619608

@@ -642,7 +631,7 @@ Object.defineProperty(Phaser.Image.prototype, "frameName", {
642631

643632
set: function(value) {
644633

645-
if (value !== this.frameName && this.game.cache.isSpriteSheet(this.key))
634+
if (value !== this.frameName)
646635
{
647636
var frameData = this.game.cache.getFrameData(this.key);
648637

src/loader/Cache.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Phaser.Cache.prototype = {
253253
*/
254254
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax, margin, spacing) {
255255

256-
this._images[key] = { url: url, data: data, spriteSheet: true, frameWidth: frameWidth, frameHeight: frameHeight, margin: margin, spacing: spacing };
256+
this._images[key] = { url: url, data: data, frameWidth: frameWidth, frameHeight: frameHeight, margin: margin, spacing: spacing };
257257

258258
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
259259
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
@@ -289,7 +289,7 @@ Phaser.Cache.prototype = {
289289
*/
290290
addTextureAtlas: function (key, url, data, atlasData, format) {
291291

292-
this._images[key] = { url: url, data: data, spriteSheet: true };
292+
this._images[key] = { url: url, data: data };
293293

294294
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
295295
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
@@ -322,7 +322,7 @@ Phaser.Cache.prototype = {
322322
*/
323323
addBitmapFont: function (key, url, data, xmlData, xSpacing, ySpacing) {
324324

325-
this._images[key] = { url: url, data: data, spriteSheet: true };
325+
this._images[key] = { url: url, data: data };
326326

327327
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
328328
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
@@ -422,9 +422,11 @@ Phaser.Cache.prototype = {
422422
*/
423423
addImage: function (key, url, data) {
424424

425-
this._images[key] = { url: url, data: data, spriteSheet: false };
425+
this._images[key] = { url: url, data: data };
426426

427427
this._images[key].frame = new Phaser.Frame(0, 0, 0, data.width, data.height, key, this.game.rnd.uuid());
428+
this._images[key].frameData = new Phaser.FrameData();
429+
this._images[key].frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url, this.game.rnd.uuid()));
428430

429431
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
430432
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
@@ -859,7 +861,7 @@ Phaser.Cache.prototype = {
859861
*/
860862
getFrameData: function (key) {
861863

862-
if (this._images[key] && this._images[key].frameData)
864+
if (this._images[key])
863865
{
864866
return this._images[key].frameData;
865867
}
@@ -878,7 +880,6 @@ Phaser.Cache.prototype = {
878880

879881
if (this._images[key])
880882
{
881-
this._images[key].spriteSheet = true;
882883
this._images[key].frameData = frameData;
883884
}
884885

@@ -893,7 +894,7 @@ Phaser.Cache.prototype = {
893894
*/
894895
getFrameByIndex: function (key, frame) {
895896

896-
if (this._images[key] && this._images[key].frameData)
897+
if (this._images[key])
897898
{
898899
return this._images[key].frameData.getFrame(frame);
899900
}
@@ -910,7 +911,7 @@ Phaser.Cache.prototype = {
910911
*/
911912
getFrameByName: function (key, frame) {
912913

913-
if (this._images[key] && this._images[key].frameData)
914+
if (this._images[key])
914915
{
915916
return this._images[key].frameData.getFrameByName(frame);
916917
}
@@ -927,7 +928,7 @@ Phaser.Cache.prototype = {
927928
*/
928929
getFrame: function (key) {
929930

930-
if (this._images[key] && this._images[key].spriteSheet === false)
931+
if (this._images[key])
931932
{
932933
return this._images[key].frame;
933934
}
@@ -1042,20 +1043,20 @@ Phaser.Cache.prototype = {
10421043
},
10431044

10441045
/**
1045-
* Check whether an image asset is sprite sheet or not.
1046+
* Get the number of frames in this image.
10461047
*
1047-
* @method Phaser.Cache#isSpriteSheet
1048-
* @param {string} key - Asset key of the sprite sheet you want.
1049-
* @return {boolean} True if the image is a sprite sheet.
1048+
* @method Phaser.Cache#getFrameCount
1049+
* @param {string} key - Asset key of the image you want.
1050+
* @return {integer} Then number of frames. 0 if the image is not found.
10501051
*/
1051-
isSpriteSheet: function (key) {
1052+
getFrameCount: function (key) {
10521053

10531054
if (this._images[key])
10541055
{
1055-
return this._images[key].spriteSheet;
1056+
return this._images[key].frameData._frames.length;
10561057
}
10571058

1058-
return false;
1059+
return 0;
10591060

10601061
},
10611062

0 commit comments

Comments
 (0)