Skip to content

Commit e4a505d

Browse files
committed
Lots of new Signals documentation.
1 parent e5ceb7e commit e4a505d

1 file changed

Lines changed: 47 additions & 4 deletions

File tree

src/core/Signal.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,58 @@
66
*/
77

88
/**
9-
* A Signal is an event dispatch mechanism that supports broadcasting to multiple listeners.
9+
* Signals are what Phaser uses to handle events and event dispatching.
10+
* You can listen for a Signal by binding a callback / function to it.
11+
* This is done by using either `Signal.add` or `Signal.addOnce`.
1012
*
11-
* Event listeners are uniquely identified by the listener/callback function and the context.
13+
* For example you can listen for a touch or click event from the Input Manager
14+
* by using its `onDown` Signal:
15+
*
16+
* `game.input.onDown.add(function() { ... });`
17+
*
18+
* Rather than inline your function, you can pass a reference:
19+
*
20+
* `game.input.onDown.add(clicked, this);`
21+
* `function clicked () { ... }`
22+
*
23+
* In this case the second argument (`this`) is the context in which your function should be called.
24+
*
25+
* Now every time the InputManager dispatches the `onDown` signal (or event), your function
26+
* will be called.
27+
*
28+
* Very often a Signal will send arguments to your function.
29+
* This is specific to the Signal itself.
30+
* If you're unsure then check the documentation, or failing that simply do:
31+
*
32+
* `Signal.add(function() { console.log(arguments); })`
33+
*
34+
* and it will log all of the arguments your function received from the Signal.
35+
*
36+
* Sprites have lots of default signals you can listen to in their Events class, such as:
37+
*
38+
* `sprite.events.onKilled`
1239
*
40+
* Which is called automatically whenever the Sprite is killed.
41+
* There are lots of other events, see the Events component for a list.
42+
*
43+
* As well as listening to pre-defined Signals you can also create your own:
44+
*
45+
* `var mySignal = new Phaser.Signal();`
46+
*
47+
* This creates a new Signal. You can bind a callback to it:
48+
*
49+
* `mySignal.add(myCallback, this);`
50+
*
51+
* and then finally when ready you can dispatch the Signal:
52+
*
53+
* `mySignal.dispatch(your arguments);`
54+
*
55+
* And your callback will be invoked. See the dispatch method for more details.
56+
*
1357
* @class Phaser.Signal
1458
* @constructor
1559
*/
16-
Phaser.Signal = function () {
17-
};
60+
Phaser.Signal = function () {};
1861

1962
Phaser.Signal.prototype = {
2063

0 commit comments

Comments
 (0)