Jump to content

Callback questions


Lycake
 Share

Recommended Posts

Hello!

 

I'm new to Phaser and web development in general, so please bear with me.

 

I was trying to use callbacks but after seeing some examples I have two questions. First I add a callback like this:

game.input.addMoveCallback(mouseMove, null);

It works, but the Docs refer to the second parameter as context. What does it mean and how is it used?

 

 

Now for the function I use

function mouseMove(pointer, x, y){	...}

I've seen something similar in an example but I was wondering how I can figure out the parameters for these functions myself. How do I know there are three parameters and in that order? I couldn't find a reference to them.

 

Thank you very much!

Link to comment
Share on other sites

Here's the link to the addMoveCallback documentation. It says:

 

The callback will be sent 4 parameters: The Pointer that moved, the x position of the pointer, the y position and the down state.

 

 

BUT, if the docs aren't giving you enough information you can add "console.log(arguments);" as the first line in your callback. You can then open the dev console of your browser and see what got passed to the function. "arguments" is a special keyword in JavaScript then is populated inside functions with a thing that's *almost* an array and contains all the arguments in order.

 

In JavaScript, the "this" of a function is very easily changed or lost. Take this code:

var o = {  message: 'Hey there!',  f: function() {    alert(this.message);  }};

If the next line is "o.f();" there will be an alert saying "Hey there!". That's straightforward.

 

But if I run this:

o.f.call({message: 'catpants'});

I change the "this" inside the function "f" to be the object I just passed to "call". Check out the docs for call for more info.

 

JavaScript has another method on the function prototype called "bind" that lets you set the "this" for a function ahead of time but it has bad performance implications at the moment in most browsers so you're going to end up passing the function and its context a lot in Phaser.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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