Jump to content

Error in Sprite.crop


Recommended Posts

While using Sprite.crop I noticed that changing crop.width doesn't always work. For example, the following code won't have any effect:

var sprite = game.add.sprite(0, 0, 'someSprite');sprite.crop = new Phaser.Rectangle(0, 0, sprite.width, sprite.height);sprite.crop.width = v; // some value v

Now the following code will work without problem:

var sprite = game.add.sprite(0, 0, 'someSprite');sprite.crop = new Phaser.Rectangle(0, 0, sprite.width, sprite.height);sprite.crop = sprite.crop; // adding this line fixes the problemsprite.crop.width = v; // some value v

The problem is inside crop's "set" function:

set: function (value) {  if (value instanceof Phaser.Rectangle)  {    if (this._cropUUID == null)    {      this._cropUUID = this.game.rnd.uuid();      PIXI.TextureCache[this._cropUUID] = new PIXI.Texture(        PIXI.BaseTextureCache[this.key], {                    x: value.x,                    y: value.y,                    width: value.width,                    height: value.height        }      );    }    else    {      PIXI.TextureCache[this._cropUUID].frame = value;    }    this._cropRect = value;    this.setTexture(PIXI.TextureCache[this._cropUUID]);  }}

The first time we set a crop rectangle R, sprite._cropRect is assigned R but in PIXI texture cache we actually create a new frame instance. So if we set sprite.crop.width we do change the width of _cropRect but not the width of the texture's frame.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...