Skip to content

Commit efcbd1e

Browse files
committed
Added helper methods for setting body collision callbacks
1 parent f64d3e5 commit efcbd1e

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

src/physics/matter-js/components/Collision.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,97 @@ var Collision = {
8181

8282
this.body.collisionFilter.mask = flags;
8383

84+
return this;
85+
},
86+
87+
/**
88+
* The callback is sent a `Phaser.Types.Physics.Matter.MatterCollisionData` object.
89+
*
90+
* This does not change the bodies collision category, group or filter. Those must be set in addition
91+
* to the callback.
92+
*
93+
* @method Phaser.Physics.Matter.Components.Collision#setOnCollide
94+
* @since 3.22.0
95+
*
96+
* @param {function} callback - The callback to invoke when this body starts colliding with another.
97+
*
98+
* @return {Phaser.GameObjects.GameObject} This Game Object.
99+
*/
100+
setOnCollide: function (callback)
101+
{
102+
this.body.onCollideCallback = callback;
103+
104+
return this;
105+
},
106+
107+
/**
108+
* The callback is sent a `Phaser.Types.Physics.Matter.MatterCollisionData` object.
109+
*
110+
* This does not change the bodies collision category, group or filter. Those must be set in addition
111+
* to the callback.
112+
*
113+
* @method Phaser.Physics.Matter.Components.Collision#setOnCollideEnd
114+
* @since 3.22.0
115+
*
116+
* @param {function} callback - The callback to invoke when this body stops colliding with another.
117+
*
118+
* @return {Phaser.GameObjects.GameObject} This Game Object.
119+
*/
120+
setOnCollideEnd: function (callback)
121+
{
122+
this.body.onCollideEndCallback = callback;
123+
124+
return this;
125+
},
126+
127+
/**
128+
* The callback is sent a `Phaser.Types.Physics.Matter.MatterCollisionData` object.
129+
*
130+
* This does not change the bodies collision category, group or filter. Those must be set in addition
131+
* to the callback.
132+
*
133+
* @method Phaser.Physics.Matter.Components.Collision#setOnCollideActive
134+
* @since 3.22.0
135+
*
136+
* @param {function} callback - The callback to invoke for the duration of this body colliding with another.
137+
*
138+
* @return {Phaser.GameObjects.GameObject} This Game Object.
139+
*/
140+
setOnCollideActive: function (callback)
141+
{
142+
this.body.onCollideActiveCallback = callback;
143+
144+
return this;
145+
},
146+
147+
/**
148+
* The callback is sent a reference to the other body, along with a `Phaser.Types.Physics.Matter.MatterCollisionData` object.
149+
*
150+
* This does not change the bodies collision category, group or filter. Those must be set in addition
151+
* to the callback.
152+
*
153+
* @method Phaser.Physics.Matter.Components.Collision#setOnCollideWith
154+
* @since 3.22.0
155+
*
156+
* @param {(MatterJS.Body|MatterJS.Body[])} body - The body, or an array of bodies, to test for collisions with.
157+
* @param {function} callback - The callback to invoke when this body collides with the given body or bodies.
158+
*
159+
* @return {Phaser.GameObjects.GameObject} This Game Object.
160+
*/
161+
setOnCollideWith: function (body, callback)
162+
{
163+
if (!Array.isArray(body))
164+
{
165+
body = [ body ];
166+
}
167+
168+
for (var i = 0; i < body.length; i++)
169+
{
170+
var src = (body[i].hasOwnProperty('body')) ? body[i].body : body[i];
171+
172+
this.body.setOnCollideWith(src, callback);
173+
}
174+
84175
return this;
85176
}
86177

0 commit comments

Comments
 (0)