Skip to content

Commit de07f5b

Browse files
committed
Testing out Frame and Animation clone features.
1 parent 8bc5ba8 commit de07f5b

5 files changed

Lines changed: 145 additions & 17 deletions

File tree

src/animation/AnimationManager.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,50 @@ Phaser.AnimationManager.prototype = {
115115
return true;
116116
},
117117

118+
/**
119+
* Loads FrameData into the internal temporary vars and resets the frame index to zero.
120+
* This is called automatically when a new Sprite is created.
121+
*
122+
* @method Phaser.AnimationManager#copyFrameData
123+
* @private
124+
* @param {Phaser.FrameData} frameData - The FrameData set to load.
125+
* @param {string|number} frame - The frame to default to.
126+
* @return {boolean} Returns `true` if the frame data was loaded successfully, otherwise `false`
127+
*/
128+
copyFrameData: function (frameData, frame) {
129+
130+
this._frameData = frameData.clone();
131+
132+
if (this.isLoaded)
133+
{
134+
// We need to update the frameData that the animations are using
135+
for (var anim in this._anims)
136+
{
137+
this._anims[anim].updateFrameData(this._frameData);
138+
}
139+
}
140+
141+
if (typeof frame === 'undefined' || frame === null)
142+
{
143+
this.frame = 0;
144+
}
145+
else
146+
{
147+
if (typeof frame === 'string')
148+
{
149+
this.frameName = frame;
150+
}
151+
else
152+
{
153+
this.frame = frame;
154+
}
155+
}
156+
157+
this.isLoaded = true;
158+
159+
return true;
160+
},
161+
118162
/**
119163
* Adds a new animation under the given key. Optionally set the frames, frame rate and loop.
120164
* Animations added in this way are played back with the play function.

src/animation/Frame.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ Phaser.Frame.prototype = {
165165

166166
},
167167

168+
/**
169+
* Clones this Frame into a new Phaser.Frame object and returns it.
170+
* Note that all properties are cloned, including the name, index and UUID.
171+
*
172+
* @method clone
173+
* @return {Phaser.Frame} An exact copy of this Frame object.
174+
*/
175+
clone: function () {
176+
177+
var output = new Phaser.Frame(this.index, this.x, this.y, this.width, this.height, this.name, this.uuid);
178+
179+
for (var prop in this)
180+
{
181+
if (this.hasOwnProperty(prop))
182+
{
183+
output[prop] = this[prop];
184+
}
185+
}
186+
187+
return output;
188+
189+
},
190+
168191
/**
169192
* Returns a Rectangle set to the dimensions of this Frame.
170193
*

src/animation/FrameData.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ Phaser.FrameData.prototype = {
105105

106106
},
107107

108+
/**
109+
* Makes a copy of this FrameData including copies (not references) to all of the Frames it contains.
110+
*
111+
* @method clone
112+
* @return {Phaser.FrameData} A clone of this object, including clones of the Frame objects it contains.
113+
*/
114+
clone: function () {
115+
116+
var output = new Phaser.FrameData();
117+
118+
// No input array, so we loop through all frames
119+
for (var i = 0; i < this._frames.length; i++)
120+
{
121+
output._frames.push(this._frames[i].clone());
122+
}
123+
124+
for (var i = 0; i < this._frameNames.length; i++)
125+
{
126+
output._frameNames.push(this._frameNames[i]);
127+
}
128+
129+
return output;
130+
131+
},
132+
108133
/**
109134
* Returns a range of frames based on the given start and end frame indexes and returns them in an Array.
110135
*

src/gameobjects/BitmapData.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -762,29 +762,32 @@ Phaser.BitmapData.prototype = {
762762
*/
763763
blit: function (image, source, dest, alpha, transform, blendMode) {
764764

765+
var setAlpha = false;
766+
765767
if (typeof image === 'string')
766768
{
767769
image = this.game.cache.getImage(image);
768770
}
769771

770-
if (typeof transform === 'undefined')
772+
if (typeof alpha === 'number')
771773
{
772-
var transform = { scaleX: 1, skewX: 0, skewY: 0, scaleY: 1, translateX: 0, translateY: 0 };
773-
}
774+
if (alpha <= 0)
775+
{
776+
// No point doing anything if alpha is zero
777+
return;
778+
}
779+
else if (alpha > 1)
780+
{
781+
// Sanity cap
782+
alpha = 1;
783+
}
774784

775-
if (typeof alpha === 'undefined')
776-
{
777-
alpha = null;
778-
}
779-
else if (alpha <= 0)
780-
{
781-
// No point doing anything if alpha is zero
782-
return;
785+
setAlpha = true;
783786
}
784-
else if (alpha > 1)
787+
788+
if (typeof transform === 'undefined' || transform === null)
785789
{
786-
// Sanity cap
787-
alpha = 1;
790+
var transform = Phaser.BitmapData.getTransform();
788791
}
789792

790793
if (typeof blendMode === 'undefined')
@@ -811,23 +814,30 @@ Phaser.BitmapData.prototype = {
811814
}
812815
}
813816

814-
if (alpha)
817+
this.context.save();
818+
819+
if (setAlpha)
815820
{
816821
var prevAlpha = this.context.globalAlpha;
817822
this.context.globalAlpha = alpha;
818823
}
819824

820-
// this.context.globalCompositeOperation = blendMode;
825+
var comp = this.context.globalCompositeOperation;
826+
this.context.globalCompositeOperation = blendMode;
821827

822828
this.context.setTransform(transform.scaleX, transform.skewX, transform.skewY, transform.scaleY, transform.translateX, transform.translateY);
823829

824830
this.context.drawImage(src, sx + source.x, sy + source.y, source.width, source.height, dest.x, dest.y, dest.width, dest.height);
825831

826-
if (alpha)
832+
this.context.restore();
833+
834+
if (setAlpha)
827835
{
828836
this.context.globalAlpha = prevAlpha;
829837
}
830838

839+
this.context.globalCompositeOperation = comp;
840+
831841
this.dirty = true;
832842

833843
},
@@ -1122,4 +1132,29 @@ Phaser.BitmapData.prototype = {
11221132

11231133
};
11241134

1135+
/**
1136+
* Gets a JavaScript object that has 6 properties set that are used by BitmapData in a transform.
1137+
*
1138+
* @method Phaser.BitmapData.getTransform
1139+
* @param {number} translateX - The x translate value.
1140+
* @param {number} translateY - The y translate value.
1141+
* @param {number} scaleX - The scale x value.
1142+
* @param {number} scaleY - The scale y value.
1143+
* @param {number} skewX - The skew x value.
1144+
* @param {number} skewY - The skew y value.
1145+
* @return {Object} A JavaScript object containing all of the properties BitmapData needs for transforms.
1146+
*/
1147+
Phaser.BitmapData.getTransform = function (translateX, translateY, scaleX, scaleY, skewX, skewY) {
1148+
1149+
if (typeof translateX !== 'number') { translateX = 0; }
1150+
if (typeof translateY !== 'number') { translateY = 0; }
1151+
if (typeof scaleX !== 'number') { scaleX = 1; }
1152+
if (typeof scaleY !== 'number') { scaleY = 1; }
1153+
if (typeof skewX !== 'number') { skewX = 0; }
1154+
if (typeof skewY !== 'number') { skewY = 0; }
1155+
1156+
return { sx: scaleX, sy: scaleY, scaleX: scaleX, scaleY: scaleY, skewX: skewX, skewY: skewY, translateX: translateX, translateY: translateY, tx: translateX, ty: translateY };
1157+
1158+
};
1159+
11251160
Phaser.BitmapData.prototype.constructor = Phaser.BitmapData;

src/gameobjects/Sprite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ Phaser.Sprite.prototype.loadTexture = function (key, frame, stopAnimation) {
378378
}
379379
else if (key instanceof Phaser.BitmapData)
380380
{
381+
// This works from a reference, which probably isn't what we need here
381382
this.setTexture(key.texture);
382383
}
383384
else if (key instanceof PIXI.Texture)

0 commit comments

Comments
 (0)