Skip to content

Commit 38eca4d

Browse files
committed
Filter.addToWorld allows you to quickly create a Phaser.Image object at the given position and size, with the Filter ready applied to it. This can eliminate lots of duplicate code.
1 parent 37fc327 commit 38eca4d

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ If you are an exceptional JavaScript developer and would like to join the Phaser
262262
* Group.resetChild is a new method that allows you to call both `child.reset` and/or `child.loadTexture` on the given child object. This is used internally by `getFirstDead` and similar, but is made public so you can use it as a group iteration callback. Note that the child must have public `reset` and `loadTexture` methods to be valid for the call.
263263
* Group.getFirstDead, Group.getFirstAlive and Group.getFirstExists all have new optional arguments: `createIfNull`, `x`, `y`, `key` and `frame`. If the method you call cannot find a matching child (i.e. getFirstDead cannot find any dead children) then the optional `createIfNull` allows you to instantly create a new child in the group using the position and texture arguments to do so. This allows you to always get a child back from the Group and remove the need to do null checks and Group inserts from your game code. The same arguments can also be used in a different way: if `createIfNull` is false AND you provide the extra arguments AND a child is found then it will be passed to the new `Group.resetChild` method. This allows you to retrieve a child from the Group and have it reset and instantly ready for use in your game without any extra code.
264264
* P2.Body.removeCollisionGroup allows you to remove the given CollisionGroup, or array of CollisionGroups, from the list of groups that a body will collide with and updates the collision masks (thanks @Garbanas #2047)
265+
* Filter.addToWorld allows you to quickly create a Phaser.Image object at the given position and size, with the Filter ready applied to it. This can eliminate lots of duplicate code.
265266

266267
### Updates
267268

src/core/Filter.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,60 @@ Phaser.Filter.prototype = {
143143

144144
},
145145

146+
/**
147+
* Creates a new Phaser.Image object using a blank texture and assigns
148+
* this Filter to it. The image is then added to the world.
149+
*
150+
* If you don't provide width and height values then Filter.width and Filter.height are used.
151+
*
152+
* If you do provide width and height values then this filter will be resized to match those
153+
* values.
154+
*
155+
* @method Phaser.Filter#addToWorld
156+
* @param {number} [x=0] - The x coordinate to place the Image at.
157+
* @param {number} [y=0] - The y coordinate to place the Image at.
158+
* @param {number} [width] - The width of the Image. If not specified (or null) it will use Filter.width. If specified Filter.width will be set to this value.
159+
* @param {number} [height] - The height of the Image. If not specified (or null) it will use Filter.height. If specified Filter.height will be set to this value.
160+
* @param {number} [anchorX=0] - Set the x anchor point of the Image. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
161+
* @param {number} [anchorY=0] - Set the y anchor point of the Image. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
162+
* @return {Phaser.Image} The newly added Image object.
163+
*/
164+
addToWorld: function (x, y, width, height, anchorX, anchorY) {
165+
166+
if (anchorX === undefined) { anchorX = 0; }
167+
if (anchorY === undefined) { anchorY = 0; }
168+
169+
if (width !== undefined && width !== null)
170+
{
171+
this.width = width;
172+
}
173+
else
174+
{
175+
width = this.width;
176+
}
177+
178+
if (height !== undefined && height !== null)
179+
{
180+
this.height = height;
181+
}
182+
else
183+
{
184+
height = this.height;
185+
}
186+
187+
var image = this.game.add.image(x, y, '__default');
188+
189+
image.width = width;
190+
image.height = height;
191+
192+
image.anchor.set(anchorX, anchorY);
193+
194+
image.filters = [ this ];
195+
196+
return image;
197+
198+
},
199+
146200
/**
147201
* Clear down this Filter and null out references
148202
* @method Phaser.Filter#destroy

typescript/phaser.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="pixi.d.ts" />
22
/// <reference path="p2.d.ts" />
33

4-
// Type definitions for Phaser 2.4.4+ 2015-Aug-28
4+
// Type definitions for Phaser 2.4.4+ 2015-Sep-02
55
// Project: https://github.com/photonstorm/phaser
66

77
declare class Phaser {
@@ -963,6 +963,7 @@ declare module Phaser {
963963
uniforms: any;
964964
width: number;
965965

966+
addToWorld(x?: number, y?: number, width?: number, height?: number, anchorX?: number, anchorY?: number): Phaser.Image;
966967
apply(frameBuffer: WebGLFramebuffer): void;
967968
destroy(): void;
968969
init(...args: any[]): void;

0 commit comments

Comments
 (0)