jjwallace Posted March 2, 2016 Share Posted March 2, 2016 //Create all of our creatures for (var i = 0; i < 30; i++){ creature.events.onInputDown.add(this.listenerClick, this); console.log(i); } // Essential i want to take var i from above and bring it to the function below. How do i do this? listenerClick: function (sprite, pointer) { console.log(sprite); creature[sprite].scale.set(0.0); }, Link to comment Share on other sites More sharing options...
rich Posted March 2, 2016 Share Posted March 2, 2016 Here, from the API docs this should help (as it shows how to pass extra arguments) /** * Add an event listener for this signal. * * An event listener is a callback with a related context and priority. * * You can optionally provide extra arguments which will be passed to the callback after any internal parameters. * * For example: `Phaser.Key.onDown` when dispatched will send the Phaser.Key object that caused the signal as the first parameter. * Any arguments you've specified after `priority` will be sent as well: * * `fireButton.onDown.add(shoot, this, 0, 'lazer', 100);` * * When onDown dispatches it will call the `shoot` callback passing it: `Phaser.Key, 'lazer', 100`. * * Where the first parameter is the one that Key.onDown dispatches internally and 'lazer', * and the value 100 were the custom arguments given in the call to 'add'. * * @method Phaser.Signal#add * @param {function} listener - The function to call when this Signal is dispatched. * @param {object} [listenerContext] - The context under which the listener will be executed (i.e. the object that should represent the `this` variable). * @param {number} [priority] - The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added (default = 0) * @param {...any} [args=(none)] - Additional arguments to pass to the callback (listener) function. They will be appended after any arguments usually dispatched. * @return {Phaser.SignalBinding} An Object representing the binding between the Signal and listener. */ Link to comment Share on other sites More sharing options...
jjwallace Posted March 2, 2016 Author Share Posted March 2, 2016 Awesome! It worked great, thanks Rich! Link to comment Share on other sites More sharing options...
Recommended Posts