|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | /** |
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`. |
10 | 12 | * |
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` |
12 | 39 | * |
| 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 | +* |
13 | 57 | * @class Phaser.Signal |
14 | 58 | * @constructor |
15 | 59 | */ |
16 | | -Phaser.Signal = function () { |
17 | | -}; |
| 60 | +Phaser.Signal = function () {}; |
18 | 61 |
|
19 | 62 | Phaser.Signal.prototype = { |
20 | 63 |
|
|
0 commit comments