diff --git a/examples/crop.php b/examples/crop.php new file mode 100644 index 000000000..598fb4acc --- /dev/null +++ b/examples/crop.php @@ -0,0 +1,46 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/crop2.php b/examples/crop2.php new file mode 100644 index 000000000..7df1b9965 --- /dev/null +++ b/examples/crop2.php @@ -0,0 +1,46 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/examples/crop3.php b/examples/crop3.php new file mode 100644 index 000000000..45551cd33 --- /dev/null +++ b/examples/crop3.php @@ -0,0 +1,48 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/gameobjects/GameObjectFactory.js b/src/gameobjects/GameObjectFactory.js index 2c5a221c4..17783ff93 100644 --- a/src/gameobjects/GameObjectFactory.js +++ b/src/gameobjects/GameObjectFactory.js @@ -21,13 +21,13 @@ Phaser.GameObjectFactory.prototype = { * * @param x {number} X position of the new sprite. * @param y {number} Y position of the new sprite. - * @param [texture] {string|RenderTexture} The image key as defined in the Game.Cache to use as the texture for this sprite OR a RenderTexture + * @param [key] {string|RenderTexture} The image key as defined in the Game.Cache to use as the texture for this sprite OR a RenderTexture * @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name. * @returns {Sprite} The newly created sprite object. */ - sprite: function (x, y, texture, frame) { + sprite: function (x, y, key, frame) { - return this.world.add(new Phaser.Sprite(this.game, x, y, texture, frame)); + return this.world.add(new Phaser.Sprite(this.game, x, y, key, frame)); }, @@ -36,13 +36,13 @@ Phaser.GameObjectFactory.prototype = { * * @param x {number} X position of the new sprite. * @param y {number} Y position of the new sprite. - * @param [texture] {string|RenderTexture} The image key as defined in the Game.Cache to use as the texture for this sprite OR a RenderTexture + * @param [key] {string|RenderTexture} The image key as defined in the Game.Cache to use as the texture for this sprite OR a RenderTexture * @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name. * @returns {Sprite} The newly created sprite object. */ - child: function (parent, x, y, texture, frame) { + child: function (parent, x, y, key, frame) { - var child = this.world.add(new Phaser.Sprite(this.game, x, y, texture, frame)); + var child = this.world.add(new Phaser.Sprite(this.game, x, y, key, frame)); parent.addChild(child); return child; diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index c6810c422..0d1dce61e 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -1,8 +1,8 @@ -Phaser.Sprite = function (game, x, y, texture, frame) { +Phaser.Sprite = function (game, x, y, key, frame) { x = x || 0; y = y || 0; - texture = texture || null; + key = key || null; frame = frame || null; this.game = game; @@ -23,24 +23,26 @@ Phaser.Sprite = function (game, x, y, texture, frame) { // The lifespan is decremented by game.time.elapsed each update, once it reaches zero the kill() function is called. this.lifespan = 0; - if (texture instanceof Phaser.RenderTexture) - { - PIXI.Sprite.call(this, texture); + this.key = key; - this.currentFrame = this.game.cache.getTextureFrame(texture.name); + if (key instanceof Phaser.RenderTexture) + { + PIXI.Sprite.call(this, key); + + this.currentFrame = this.game.cache.getTextureFrame(key.name); } else { - if (texture == null || this.game.cache.checkImageKey(texture) == false) + if (key == null || this.game.cache.checkImageKey(key) == false) { - texture = '__default'; + key = '__default'; } - PIXI.Sprite.call(this, PIXI.TextureCache[texture]); + PIXI.Sprite.call(this, PIXI.TextureCache[key]); - if (this.game.cache.isSpriteSheet(texture)) + if (this.game.cache.isSpriteSheet(key)) { - this.animations.loadFrameData(this.game.cache.getFrameData(texture)); + this.animations.loadFrameData(this.game.cache.getFrameData(key)); if (frame !== null) { @@ -56,7 +58,7 @@ Phaser.Sprite = function (game, x, y, texture, frame) { } else { - this.currentFrame = this.game.cache.getFrame(texture); + this.currentFrame = this.game.cache.getFrame(key); } } @@ -89,6 +91,9 @@ Phaser.Sprite = function (game, x, y, texture, frame) { */ this.anchor = new Phaser.Point(); + this._cropUUID = null; + this._cropRect = null; + this.x = x; this.y = y; @@ -204,7 +209,6 @@ Phaser.Sprite.prototype.preUpdate = function() { this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y); // If this sprite or the camera have moved then let's update everything - // It may have rotated though ... if (this.position.x != this._cache.x || this.position.y != this._cache.y) { this.position.x = this._cache.x; @@ -492,6 +496,48 @@ Object.defineProperty(Phaser.Sprite.prototype, "inCamera", { }); +Object.defineProperty(Phaser.Sprite.prototype, "crop", { + + /** + * Get the input enabled state of this Sprite. + */ + get: function () { + + return this._cropRect; + + }, + + /** + * Set the ability for this sprite to receive input events. + */ + 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]); + } + + } + +}); + Object.defineProperty(Phaser.Sprite.prototype, "inputEnabled", { /**