@@ -291,27 +291,7 @@ Phaser.Sprite.prototype.preUpdate = function() {
291291 this . _cache [ 3 ] = this . game . stage . currentRenderOrderID ++ ;
292292 }
293293
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- }
294+ this . animations . update ( ) ;
315295
316296 if ( this . body && this . body . enable )
317297 {
@@ -420,8 +400,11 @@ Phaser.Sprite.prototype.loadTexture = function (key, frame) {
420400
421401 if ( this . game . cache . isSpriteSheet ( key ) )
422402 {
403+ console . log ( 'create animation' , this . key ) ;
423404 this . key = key ;
405+ this . setTexture ( new PIXI . Texture ( PIXI . BaseTextureCache [ key ] ) ) ;
424406 this . animations . loadFrameData ( this . game . cache . getFrameData ( key ) ) ;
407+ this . textureChange = true ;
425408
426409 if ( typeof frame === 'string' )
427410 {
@@ -435,14 +418,125 @@ Phaser.Sprite.prototype.loadTexture = function (key, frame) {
435418 else
436419 {
437420 this . key = key ;
438- this . setTexture ( PIXI . TextureCache [ key ] ) ;
421+ this . setTexture ( new PIXI . Texture ( PIXI . BaseTextureCache [ key ] ) ) ;
439422 this . animations . loadFrameData ( null ) ;
440423 return ;
441424 }
442425 }
443426
444427} ;
445428
429+
430+ Phaser . Sprite . prototype . setFrame = function ( frame ) {
431+
432+ if ( frame . trimmed )
433+ {
434+ if ( this . _crop )
435+ {
436+ // Works but doesn't take crop x/y into account
437+ // this.texture.frame.x = frame.x;
438+ // this.texture.frame.y = frame.y;
439+ // this.texture.frame.width = this._crop.width - frame.spriteSourceSizeX;
440+ // this.texture.frame.height = this._crop.height - frame.spriteSourceSizeY;
441+ // this.texture.trim = new Phaser.Rectangle(frame.spriteSourceSizeX, frame.spriteSourceSizeY, frame.width, frame.height);
442+
443+ var fx = frame . x + this . _crop . x - frame . spriteSourceSizeX ;
444+
445+ if ( fx < frame . x )
446+ {
447+ fx = frame . x ;
448+ }
449+
450+ var fy = frame . y + this . _crop . y - frame . spriteSourceSizeY ;
451+
452+ if ( fy < frame . y )
453+ {
454+ fy = frame . y ;
455+ }
456+
457+ this . texture . frame . x = fx ;
458+ this . texture . frame . y = fy ;
459+
460+ var tx = 0 ;
461+ var ty = 0 ;
462+
463+ if ( this . _crop . x === 0 )
464+ {
465+ tx = frame . spriteSourceSizeX ;
466+ }
467+
468+ if ( this . _crop . y === 0 )
469+ {
470+ ty = frame . spriteSourceSizeY ;
471+ }
472+
473+ this . texture . frame . width = this . _crop . width - tx ;
474+ this . texture . frame . height = this . _crop . height - ty ;
475+
476+ this . texture . trim = new Phaser . Rectangle ( tx , ty , this . _crop . width , this . _crop . height ) ;
477+ }
478+ else
479+ {
480+ this . texture . frame . x = frame . x ;
481+ this . texture . frame . y = frame . y ;
482+ this . texture . frame . width = frame . width ;
483+ this . texture . frame . height = frame . height ;
484+ this . texture . trim = new Phaser . Rectangle ( frame . spriteSourceSizeX , frame . spriteSourceSizeY , frame . width , frame . height ) ;
485+ }
486+ }
487+ else
488+ {
489+ this . texture . frame . x = frame . x ;
490+ this . texture . frame . y = frame . y ;
491+ this . texture . frame . width = frame . width ;
492+ this . texture . frame . height = frame . height ;
493+
494+ if ( this . _crop )
495+ {
496+ this . texture . frame . x += this . _crop . x ;
497+ this . texture . frame . y += this . _crop . y ;
498+ this . texture . frame . width = this . _crop . width ;
499+ this . texture . frame . height = this . _crop . height ;
500+ }
501+ }
502+
503+ if ( this . game . renderType === Phaser . WEBGL )
504+ {
505+ PIXI . WebGLRenderer . updateTextureFrame ( this . texture ) ;
506+ }
507+
508+ } ;
509+
510+
511+ Phaser . Sprite . prototype . XsetFrame = function ( x , y , width , height ) {
512+
513+ // console.log('setFrame', this.key, x, y);
514+
515+ this . texture . frame . x = x ;
516+ this . texture . frame . y = y ;
517+ this . texture . frame . width = width ;
518+ this . texture . frame . height = height ;
519+
520+ // Apply crop?
521+
522+ if ( this . _crop )
523+ {
524+ this . texture . frame . x += this . _crop . x ;
525+ this . texture . frame . y += this . _crop . y ;
526+ this . texture . frame . width = this . _crop . width ;
527+ this . texture . frame . height = this . _crop . height ;
528+ }
529+
530+ // Needed?
531+ // this.updateFrame = true;
532+
533+ if ( this . game . renderType === Phaser . WEBGL )
534+ {
535+ PIXI . WebGLRenderer . updateTextureFrame ( this . texture ) ;
536+ }
537+
538+ } ;
539+
446540/**
447541* Crop allows you to crop the texture used to display this Sprite.
448542* Cropping takes place from the top-left of the Sprite and can be modified in real-time by providing an updated rectangle object.
@@ -458,46 +552,25 @@ Phaser.Sprite.prototype.loadTexture = function (key, frame) {
458552*/
459553Phaser . Sprite . prototype . crop = function ( rect , copy ) {
460554
461- this . _frame = { x : 0 , y : 0 , width : 0 , height : 0 } ;
462-
463- if ( typeof rect === 'undefined' || rect === null )
555+ if ( rect )
464556 {
465- this . _crop = null ;
466-
467- // Clear any crop that may be set
468- if ( this . texture . hasOwnProperty ( 'sourceWidth' ) )
557+ if ( copy )
469558 {
470- this . texture . setFrame ( new Phaser . Rectangle ( 0 , 0 , this . texture . sourceWidth , this . texture . sourceHeight ) ) ;
471- }
472- }
473- else
474- {
475- // Do we need to clone the PIXI.Texture object?
476- if ( this . texture instanceof PIXI . Texture )
477- {
478- this . _crop = rect ;
479-
480- // Yup, let's rock it ...
481- var local = { } ;
482-
483- Phaser . Utils . extend ( true , local , this . texture ) ;
484-
485- local . sourceWidth = local . width ;
486- local . sourceHeight = local . height ;
487- local . frame = rect ;
488- local . width = rect . width ;
489- local . height = rect . height ;
490-
491- this . texture = local ;
492-
493- this . texture . updateFrame = true ;
494- PIXI . Texture . frameUpdates . push ( this . texture ) ;
559+ this . _crop = new Phaser . Rectangle ( rect . x , rect . y , rect . width , rect . height ) ;
495560 }
496561 else
497562 {
498563 this . _crop = rect ;
499- this . texture . setFrame ( rect ) ;
500564 }
565+
566+ // this.setFrame(this.texture.frame.x, this.texture.frame.y, this.texture.frame.width, this.texture.frame.height);
567+ this . setFrame ( this . texture ) ;
568+ }
569+ else
570+ {
571+ this . _crop = null ;
572+ // How to reset the frame
573+ // this.setFrame(this.texture.frame.x, this.texture.frame.y, this.texture.frame.width, this.texture.frame.height);
501574 }
502575
503576} ;
0 commit comments