Skip to content

Commit b63900f

Browse files
committed
Testing frame crop support.
1 parent 62dfd56 commit b63900f

3 files changed

Lines changed: 68 additions & 23 deletions

File tree

src/animation/Animation.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -378,20 +378,21 @@ Phaser.Animation.prototype = {
378378
if (this.loop)
379379
{
380380
this._frameIndex %= this._frames.length;
381-
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
381+
// this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
382382

383-
if (this.currentFrame)
384-
{
385-
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
383+
// if (this.currentFrame)
384+
// {
385+
// this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
386386

387-
if (this._parent.__tilePattern)
388-
{
389-
this._parent.__tilePattern = false;
390-
this._parent.tilingTexture = false;
391-
}
392-
}
387+
// if (this._parent.__tilePattern)
388+
// {
389+
// this._parent.__tilePattern = false;
390+
// this._parent.tilingTexture = false;
391+
// }
392+
// }
393393

394394
this.loopCount++;
395+
// console.log('loop', this.loopCount);
395396
this._parent.events.onAnimationLoop.dispatch(this._parent, this);
396397
this.onLoop.dispatch(this._parent, this);
397398
}
@@ -400,19 +401,22 @@ Phaser.Animation.prototype = {
400401
this.complete();
401402
}
402403
}
403-
else
404+
// else
405+
// {
406+
// }
407+
408+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
409+
410+
if (this.currentFrame)
404411
{
405-
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
412+
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
406413

407-
if (this.currentFrame)
408-
{
409-
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
414+
// console.log('a1', this._parent.texture.frame, PIXI.TextureCache[this.currentFrame.uuid].frame);
410415

411-
if (this._parent.__tilePattern)
412-
{
413-
this._parent.__tilePattern = false;
414-
this._parent.tilingTexture = false;
415-
}
416+
if (this._parent.__tilePattern)
417+
{
418+
this._parent.__tilePattern = false;
419+
this._parent.tilingTexture = false;
416420
}
417421
}
418422

@@ -429,6 +433,7 @@ Phaser.Animation.prototype = {
429433
* @method Phaser.Animation#destroy
430434
*/
431435
destroy: function () {
436+
432437
this.game.onPause.remove(this.onPause, this);
433438
this.game.onResume.remove(this.onResume, this);
434439

src/core/Game.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ Phaser.Game.prototype = {
604604
{
605605
this.stage.smoothed = this.antialias;
606606

607-
Phaser.Canvas.addToDOM(this.canvas, this.parent, true);
607+
Phaser.Canvas.addToDOM(this.canvas, this.parent, false);
608608
Phaser.Canvas.setTouchAction(this.canvas);
609609
}
610610

src/gameobjects/Sprite.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ Phaser.Sprite = function (game, x, y, key, frame) {
183183
*/
184184
this._bounds = new Phaser.Rectangle();
185185

186+
/**
187+
* @property {Phaser.Rectangle} _crop - Internal cache var.
188+
* @private
189+
*/
190+
this._crop = null;
191+
192+
/**
193+
* @property {Phaser.Rectangle} _frame - Internal cache var.
194+
* @private
195+
*/
196+
this._frame = null;
197+
186198
};
187199

188200
Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
@@ -279,7 +291,27 @@ Phaser.Sprite.prototype.preUpdate = function() {
279291
this._cache[3] = this.game.stage.currentRenderOrderID++;
280292
}
281293

282-
this.animations.update();
294+
if (this.animations.update() && this._crop)
295+
{
296+
// Reset?
297+
this.texture.frame.x = this._frame.x;
298+
this.texture.frame.y = this._frame.y;
299+
this.texture.frame.width = this._frame.width;
300+
this.texture.frame.height = this._frame.height;
301+
302+
303+
this._frame.x = this.texture.frame.x;
304+
this._frame.y = this.texture.frame.y;
305+
this._frame.width = this.texture,frame.width;
306+
this._frame.height = this.texture,frame.height;
307+
308+
this.texture.frame.x += this._crop.x;
309+
this.texture.frame.y += this._crop.y;
310+
this.texture.frame.width = this._crop.width;
311+
this.texture.frame.height = this._crop.height;
312+
313+
// console.log('a2', this.texture.frame);
314+
}
283315

284316
if (this.body && this.body.enable)
285317
{
@@ -422,11 +454,16 @@ Phaser.Sprite.prototype.loadTexture = function (key, frame) {
422454
* @method Phaser.Sprite#crop
423455
* @memberof Phaser.Sprite
424456
* @param {Phaser.Rectangle} rect - The Rectangle to crop the Sprite to. Pass null or no parameters to clear a previously set crop rectangle.
457+
* @param {boolean} [copy=false] - Should the Sprite store a local copy of the Rectangle object?
425458
*/
426-
Phaser.Sprite.prototype.crop = function(rect) {
459+
Phaser.Sprite.prototype.crop = function(rect, copy) {
460+
461+
this._frame = { x: 0, y: 0, width: 0, height: 0 };
427462

428463
if (typeof rect === 'undefined' || rect === null)
429464
{
465+
this._crop = null;
466+
430467
// Clear any crop that may be set
431468
if (this.texture.hasOwnProperty('sourceWidth'))
432469
{
@@ -438,6 +475,8 @@ Phaser.Sprite.prototype.crop = function(rect) {
438475
// Do we need to clone the PIXI.Texture object?
439476
if (this.texture instanceof PIXI.Texture)
440477
{
478+
this._crop = rect;
479+
441480
// Yup, let's rock it ...
442481
var local = {};
443482

@@ -456,6 +495,7 @@ Phaser.Sprite.prototype.crop = function(rect) {
456495
}
457496
else
458497
{
498+
this._crop = rect;
459499
this.texture.setFrame(rect);
460500
}
461501
}

0 commit comments

Comments
 (0)