Skip to content

Commit 84c7960

Browse files
committed
The Quad Game Object now has a new setFrame method that allows you to change the frame being rendered by the Quad, including using frames that are part of a texture atlas. Fix phaserjs#3161
1 parent ecd9986 commit 84c7960

3 files changed

Lines changed: 69 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ There is a new Game Object Component called `TextureCrop`. It replaces the Textu
108108
* `TransformMatrix.e` and `TransformMatrix.f` are two new properties that are an alias for the `tx` and `ty` values.
109109
* `Graphics.arc` has a new optional argument `overshoot`. This is a small value that is added onto the end of the `endAngle` and allows you to extend the arc further than the default 360 degrees. You may wish to do this if you're trying to draw an arc with an especially thick line stroke, to ensure there are no gaps. Fix #3798 (thanks @jjalonso)
110110
* The TextureManager Sprite Sheet Parser will now throw a concise console warning if you specify invalid frame sizes that would result in no frames being generated (thanks @andygroff)
111+
* The `Quad` Game Object now has a new `setFrame` method that allows you to change the frame being rendered by the Quad, including using frames that are part of a texture atlas. Fix #3161 (thanks @halgorithm)
111112

112113
### Bug Fixes
113114

src/gameobjects/mesh/Mesh.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ var Mesh = new Class({
7373
{
7474
GameObject.call(this, scene, 'Mesh');
7575

76-
this.setTexture(texture, frame);
77-
this.setPosition(x, y);
78-
this.setSizeToFrame();
79-
this.setOrigin();
80-
this.initPipeline('TextureTintPipeline');
81-
8276
if (vertices.length !== uv.length)
8377
{
8478
throw new Error('Mesh Vertex count must match UV count');
@@ -159,6 +153,12 @@ var Mesh = new Class({
159153
* @since 3.11.0
160154
*/
161155
this.tintFill = false;
156+
157+
this.setTexture(texture, frame);
158+
this.setPosition(x, y);
159+
this.setSizeToFrame();
160+
this.setOrigin();
161+
this.initPipeline('TextureTintPipeline');
162162
}
163163

164164
});

src/gameobjects/quad/Quad.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var Quad = new Class({
5454
0, 0, // br
5555
0, 0 // tr
5656
];
57+
5758
var uv = [
5859
0, 0, // tl
5960
0, 1, // bl
@@ -62,6 +63,7 @@ var Quad = new Class({
6263
1, 1, // br
6364
1, 0 // tr
6465
];
66+
6567
var colors = [
6668
0xffffff, // tl
6769
0xffffff, // bl
@@ -70,6 +72,7 @@ var Quad = new Class({
7072
0xffffff, // br
7173
0xffffff // tr
7274
];
75+
7376
var alphas = [
7477
1, // tl
7578
1, // bl
@@ -84,6 +87,65 @@ var Quad = new Class({
8487
this.resetPosition();
8588
},
8689

90+
/**
91+
* Sets the frame this Game Object will use to render with.
92+
*
93+
* The Frame has to belong to the current Texture being used.
94+
*
95+
* It can be either a string or an index.
96+
*
97+
* Calling `setFrame` will modify the `width` and `height` properties of your Game Object.
98+
* It will also change the `origin` if the Frame has a custom pivot point, as exported from packages like Texture Packer.
99+
*
100+
* @method Phaser.GameObjects.Quad#setFrame
101+
* @since 3.11.0
102+
*
103+
* @param {(string|integer)} frame - The name or index of the frame within the Texture.
104+
*
105+
* @return {this} This Game Object instance.
106+
*/
107+
setFrame: function (frame)
108+
{
109+
this.frame = this.texture.get(frame);
110+
111+
if (!this.frame.cutWidth || !this.frame.cutHeight)
112+
{
113+
this.renderFlags &= ~8;
114+
}
115+
else
116+
{
117+
this.renderFlags |= 8;
118+
}
119+
120+
frame = this.frame;
121+
122+
// TL
123+
this.uv[0] = frame.u0;
124+
this.uv[1] = frame.v0;
125+
126+
// BL
127+
this.uv[2] = frame.u0;
128+
this.uv[3] = frame.v1;
129+
130+
// BR
131+
this.uv[4] = frame.u1;
132+
this.uv[5] = frame.v1;
133+
134+
// TL
135+
this.uv[6] = frame.u0;
136+
this.uv[7] = frame.v0;
137+
138+
// BR
139+
this.uv[8] = frame.u1;
140+
this.uv[9] = frame.v1;
141+
142+
// TR
143+
this.uv[10] = frame.u1;
144+
this.uv[11] = frame.v0;
145+
146+
return this;
147+
},
148+
87149
/**
88150
* The top-left x vertex of this Quad.
89151
*

0 commit comments

Comments
 (0)