Skip to content

Commit af1508d

Browse files
committed
BitmapData.addToWorld will create a new Phaser.Image object, assign the BitmapData to be its texture, add it to the world then return it.
BitmapData.copyPixels now accepts a Sprite, Image, BitmapData, HTMLImage or string as its source.
1 parent 2219e6f commit af1508d

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Version 2.0.6 - "Jornhill" - -in development-
7272
* Phaser.Keyboard.lastKey will return the most recently pressed Key object.
7373
* RetroFont.updateOffset allows you to modify the offsetX/Y values used by the font during rendering.
7474
* ArcadePhysics.Body has a new boolean property `enable`. If `false` the body won't be checked for any collision or overlaps, or have its pre or post update methods called. Use this for easy toggling of physics bodies without having to destroy or re-create the Body object itself.
75+
* BitmapData.addToWorld will create a new Phaser.Image object, assign the BitmapData to be its texture, add it to the world then return it.
76+
* BitmapData.copyPixels now accepts a Sprite, Image, BitmapData, HTMLImage or string as its source.
7577

7678

7779
### Bug Fixes

src/gameobjects/BitmapData.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -750,27 +750,57 @@ Phaser.BitmapData.prototype = {
750750

751751
},
752752

753+
/**
754+
* Creates a new Phaser.Image object, assigns this BitmapData to be its texture, adds it to the world then returns it.
755+
*
756+
* @method Phaser.BitmapData#addToWorld
757+
* @param {number} [x=0] - The x coordinate to place the image at.
758+
* @param {number} [y=0] - The y coordinate to place the image at.
759+
* @return {Phaser.Image} The newly added Image object.
760+
*/
761+
addToWorld: function (x, y) {
762+
763+
return this.game.add.image(x, y, this);
764+
765+
},
766+
753767
/**
754768
* Copies the pixels from the source image to this BitmapData based on the given area and destination.
755769
*
756770
* @method Phaser.BitmapData#copyPixels
757-
* @param {HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
771+
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} source - The Image to copy from. If you give a string it will try and find the Image in the Game.Cache.
758772
* @param {Phaser.Rectangle} area - The Rectangle region to copy from the source image.
759-
* @param {number} destX - The destination x coordinate to copy the image to.
760-
* @param {number} destY - The destination y coordinate to copy the image to.
773+
* @param {number} x - The destination x coordinate to copy the image to.
774+
* @param {number} y - The destination y coordinate to copy the image to.
761775
*/
762-
copyPixels: function (source, area, destX, destY) {
776+
copyPixels: function (source, area, x, y) {
763777

764778
if (typeof source === 'string')
765779
{
766780
source = this.game.cache.getImage(source);
767781
}
768782

769-
if (source)
783+
var src = source;
784+
var sx = 0;
785+
var sy = 0;
786+
787+
if (source instanceof Phaser.Image || source instanceof Phaser.Sprite)
770788
{
771-
this.context.drawImage(source, area.x, area.y, area.width, area.height, destX, destY, area.width, area.height);
789+
src = source.texture.baseTexture.source;
790+
var frame = source.texture.frame;
791+
sx = frame.x;
792+
sy = frame.y;
793+
}
794+
else
795+
{
796+
if (source instanceof Phaser.BitmapData)
797+
{
798+
src = source.canvas;
799+
}
772800
}
773801

802+
this.context.drawImage(src, sx + area.x, sy + area.y, area.width, area.height, x, y, area.width, area.height);
803+
774804
this.dirty = true;
775805

776806
},

0 commit comments

Comments
 (0)