Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Event parameters


Delfil
 Share

Recommended Posts

Hi,

I'm new to Phaser and relatively new to JavaScript. I was wondering if there is there some documentation about what parameters each different event provides? For example the onDragStart event, what parameters are available for the function someFunction shown bellow? The examples I've tried (for example: drag example1 & drag example2) give me a different set of parameters without much explanation.

var sprite = game.add.sprite(x, y, imageName);
sprite.events.onDragStart.add(someFunction);

function someFunction(...) {}

 

Link to comment
Share on other sites

For onDragStart and onDragStop they are two: The first one is the sprite being dragged and the second one is the pointer describing the position of the mouse.

Be aware,  though, that the complete signature of the onDragStart.add function (as well as the add function on any signal) is:

add(listener, listenerContext, priority, args)

Where only the first parameter is mandatory. If you include any value or reference after the priority parameter, they will be sent to the callback too so you my have aditional personalized arguments sent to your function. you have an example of how to do this here.

I had the same problem so I prepared a document looking for the dispatch function that makes the call (and using the search capabilities of WebStorm). Hope it is useful. Personally I think this should be in the documentation if my memory is right I recall participating on a topic also asking for this.

Anyhow here you have it.

Phaser Signal Listener Parameters.pdf

Link to comment
Share on other sites

Thank you for your explanation.

I know what the functions do, already got something working with events, but I was wondering about the use of some of the available parameters, the onDragStart was just as example.
For example, in the pdf you included (thanks a lot for this pdf, it will be really useful), I picked the following event at random:

Quote

this.enterPortrait.dispatch(this.orientation, false, true);

I could find out what is meant with the orientation, but the 'true' and 'false' are not as descriptive. Is there some documentation on these parameters? Not specific these parameters or this function, but is this documented somewhere so that I can look them up myself?

Link to comment
Share on other sites

Documentation for the add function, describing 4 parameters (the last being variadic), it even has a helpful bit of description which is nice for api docs.

More usefully those docs also point to the code, dont worry about being new to Phaser and JS, dive into the code anyway, it'll help you learn the language, learn some patterns for using that language as well as teach you the primary goal of working out how the framework (Phaser) works. I just had a quick squizz at the functions (and, more helpfully, the accompanying code comments) and it just looks like it creates a SignalBinding object which is placed into a priority-sorted list of handlers, I didn't dive into the dispatch code but I'd guess that all that happens is that when a dispatch arrives it plucks each matching handler, in priority order, and executes them.

The comments suggest that when an even gets triggered none of the signal params refer specifically to a type of event (this is very good, decoupled design allowing the signal pattern to be generalised to many events), the params simply assign the handler, set which scope it should run in, add a priority so it can be sorted and additionally lets you set some arguments that the handler will receive when fired, I'm not clear if these are in addition to any args from the event, I'm assuming that none of them have any args but a quick look at the dispatch code should confirm (or just log out the arguments and run some dummy code).

TL;DR

Docs are here, code in here. The add function relates to a signal which is used to manage each event so there are just 4 parameters you can set to add an event listener to any event.

Link to comment
Share on other sites

Hmmm, I realized the pdf is not up to date with the last version of Phaser, I had some very old examples using Phaser 1.1 in the folder I made the search. That entry is from that phaser version  and the enterPortrait  signal doesn't exist anymore. Sorry for the confusion. I will generate the pdf again to correct that this evening (the Phaser version I'm actually using is 2.4.4 though)

Anyhow it's quite easy to generate one for the version you use in any IDE or editor you use to write your code. Just use the project wide search function with the term 'dispatch('. I include the first bracket so results from the comments are not included. No other object than signals have that member.

Just in case you don't find the way in your IDE. I did it with Notepad++, not webStorm, sorry, I was mistaken.

In Notepad++ Go to Search -> Find in Files and make a Find all. Make sure you choose the src folder of the Phaser version you downloaded. You can copy the results from the results panel and make some cleanup as some hits are from classes you are never going to use.

As for the meaning of the parameters sent by the signal dispatch function, the link to the docs for the add function matsttyles talk about  give you some guidance and shows you an example that applies to the onDown signal, but different signals dispatch different type and quantity of parameters.

Most of the times you'll have to dive into the code because the documentation is mostly incomplete on this matter, which honestly I understand. It would be nice to have that information in the docs, though.

But of course, you can ask whatever you don't understand and we all be pleased to help you. That's what this forum is meant for

Link to comment
Share on other sites

On 6 juni 2016 at 10:10 AM, glantucan said:

Hmmm, I realized the pdf is not up to date with the last version of Phaser, I had some very old examples using Phaser 1.1 in the folder I made the search. That entry is from that phaser version  and the enterPortrait  signal doesn't exist anymore. Sorry for the confusion. I will generate the pdf again to correct that this evening (the Phaser version I'm actually using is 2.4.4 though)

The specific method doesn't matter, I used this as an example, and was not confused.

Thank you both for your tips and references, they will be helpful.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...