Skip to content

Commit 9b9baa8

Browse files
committed
Added Image.frame and Image.frameName support in.
1 parent d583b36 commit 9b9baa8

2 files changed

Lines changed: 142 additions & 18 deletions

File tree

examples/wip/frame.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.spritesheet('mummy', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18);
7+
game.load.atlas('seacreatures', 'assets/sprites/seacreatures_json.png', 'assets/sprites/seacreatures_json.json');
8+
9+
}
10+
11+
var mummy;
12+
var jelly;
13+
14+
function create() {
15+
16+
// Testing both sprite sheets and texture atlases with a Phaser.Image
17+
18+
mummy = game.add.image(200, 200, 'mummy');
19+
20+
jelly = game.add.image(200, 300, 'seacreatures', 'blueJellyfish0000');
21+
22+
game.input.onDown.add(changeFrame, this);
23+
24+
}
25+
26+
function changeFrame() {
27+
28+
if (jelly.frameName === 'blueJellyfish0000')
29+
{
30+
jelly.frameName = 'crab10000';
31+
}
32+
else if (jelly.frameName === 'crab10000')
33+
{
34+
jelly.frameName = 'purpleFish0000';
35+
}
36+
else
37+
{
38+
jelly.frameName = 'blueJellyfish0000';
39+
}
40+
41+
}
42+
43+
function update() {
44+
45+
// You could animate an Image like this! But it makes more sense to use a Sprite
46+
if (mummy.frame < 17)
47+
{
48+
mummy.frame++;
49+
}
50+
else
51+
{
52+
mummy.frame = 0;
53+
}
54+
55+
}

src/gameobjects/Image.js

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77
/**
88
* @class Phaser.Image
99
*
10-
* @classdesc Create a new `Image` object.
11-
*
12-
* At its most basic a Sprite consists of a set of coordinates and a texture that is rendered to the canvas.
10+
* @classdesc Create a new `Image` object. An Image is a light-weight object you can use to display anything that doesn't need physics, animation or input events.
11+
* It can still rotate, scale and crop. This makes it perfect for logos, backgrounds and other non-Sprite graphics.
1312
*
1413
* @constructor
1514
* @param {Phaser.Game} game - A reference to the currently running game.
16-
* @param {number} x - The x coordinate (in world space) to position the Sprite at.
17-
* @param {number} y - The y coordinate (in world space) to position the Sprite at.
18-
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
19-
* @param {string|number} frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
15+
* @param {number} x - The x coordinate of the Imaget. The coordinate is relative to any parent container this Image may be in.
16+
* @param {number} y - The y coordinate of the Image. The coordinate is relative to any parent container this Image may be in.
17+
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - The texture used by the Image during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture, BitmapData or PIXI.Texture.
18+
* @param {string|number} frame - If this Image is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
2019
*/
2120
Phaser.Image = function (game, x, y, key, frame) {
2221

@@ -58,7 +57,8 @@ Phaser.Image = function (game, x, y, key, frame) {
5857
*/
5958
this.key = key;
6059

61-
this.currentFrame = new Phaser.Rectangle();
60+
this._frame = 0;
61+
this._frameName = '';
6262

6363
PIXI.Sprite.call(this, PIXI.TextureCache['__default']);
6464

@@ -161,52 +161,58 @@ Phaser.Image.prototype.postUpdate = function() {
161161
*/
162162
Phaser.Image.prototype.loadTexture = function (key, frame) {
163163

164-
this.key = key;
165164
frame = frame || 0;
166165

167166
if (key instanceof Phaser.RenderTexture)
168167
{
169-
this.key = key.name;
168+
this.key = key.key;
170169
this.setTexture(key);
171170
}
172171
else if (key instanceof Phaser.BitmapData)
173172
{
173+
this.key = key.key;
174174
this.setTexture(key.texture);
175175
}
176176
else if (key instanceof PIXI.Texture)
177177
{
178+
this.key = key;
178179
this.setTexture(key);
179180
}
180181
else
181182
{
182183
if (key === null || typeof key === 'undefined')
183184
{
184-
key = '__default';
185-
this.key = key;
186-
this.setTexture(PIXI.TextureCache[key]);
185+
this.key = '__default';
186+
this.setTexture(PIXI.TextureCache[this.key]);
187187
}
188-
else if (typeof key === 'string' && this.game.cache.checkImageKey(key) === false)
188+
else if (typeof key === 'string' && !this.game.cache.checkImageKey(key))
189189
{
190-
key = '__missing';
191-
this.key = key;
192-
this.setTexture(PIXI.TextureCache[key]);
190+
this.key = '__missing';
191+
this.setTexture(PIXI.TextureCache[this.key]);
193192
}
194193

195194
if (this.game.cache.isSpriteSheet(key))
196195
{
196+
this.key = key;
197+
197198
var frameData = this.game.cache.getFrameData(key);
198199

199200
if (typeof frame === 'string')
200201
{
202+
this._frame = 0;
203+
this._frameName = frame;
201204
this.setTexture(PIXI.TextureCache[frameData.getFrameByName(frame).uuid]);
202205
}
203206
else
204207
{
208+
this._frame = frame;
209+
this._frameName = '';
205210
this.setTexture(PIXI.TextureCache[frameData.getFrame(frame).uuid]);
206211
}
207212
}
208213
else
209214
{
215+
this.key = key;
210216
this.setTexture(PIXI.TextureCache[key]);
211217
}
212218
}
@@ -219,7 +225,7 @@ Phaser.Image.prototype.loadTexture = function (key, frame) {
219225
*
220226
* @method Phaser.Image#crop
221227
* @memberof Phaser.Image
222-
* @param {Phaser.Rectangle} rect - The Rectangle to crop the Image to. Pass as null to clear any previously set crop.
228+
* @param {Phaser.Rectangle} rect - The Rectangle to crop the Image to. Pass null or no parameters to clear a previously set crop rectangle.
223229
*/
224230
Phaser.Image.prototype.crop = function(rect) {
225231

@@ -501,3 +507,66 @@ Object.defineProperty(Phaser.Image.prototype, "inCamera", {
501507
}
502508

503509
});
510+
511+
/**
512+
* .
513+
*
514+
* @name Phaser.Image#frame
515+
* @property {boolean} frame - .
516+
*/
517+
Object.defineProperty(Phaser.Image.prototype, "frame", {
518+
519+
get: function() {
520+
521+
return this._frame;
522+
523+
},
524+
525+
set: function(value) {
526+
527+
if (value !== this.frame && this.game.cache.isSpriteSheet(this.key))
528+
{
529+
var frameData = this.game.cache.getFrameData(this.key);
530+
531+
if (frameData && value < frameData.total && frameData.getFrame(value))
532+
{
533+
this.setTexture(PIXI.TextureCache[frameData.getFrame(value).uuid]);
534+
this._frame = value;
535+
}
536+
}
537+
538+
}
539+
540+
});
541+
542+
/**
543+
* .
544+
*
545+
* @name Phaser.Image#frameName
546+
* @property {boolean} frameName - .
547+
*/
548+
Object.defineProperty(Phaser.Image.prototype, "frameName", {
549+
550+
get: function() {
551+
552+
return this._frameName;
553+
554+
},
555+
556+
set: function(value) {
557+
558+
if (value !== this.frameName && this.game.cache.isSpriteSheet(this.key))
559+
{
560+
var frameData = this.game.cache.getFrameData(this.key);
561+
562+
if (frameData && frameData.getFrameByName(value))
563+
{
564+
this.setTexture(PIXI.TextureCache[frameData.getFrameByName(value).uuid]);
565+
this._frameName = value;
566+
}
567+
}
568+
569+
}
570+
571+
});
572+

0 commit comments

Comments
 (0)