Jump to content

How to detect if the mouse was clicked on the right or left side of the game?


i_want_to_make_games
 Share

Recommended Posts

How to detect if the mouse was clicked on the right or left side of the game? If it was clicked in pixel_x < game.width / 2 then I want to call func1(), and func2() otherwise. How can I do that? I've made working just arrow at the moment. Also, will I be able to convert it to android app after I finish? Will it understand that mouse click is the same as click on the screen?

Link to comment
Share on other sites

What you want to do is attach a callback to the onDown event of the games input object.

game.input.onDown.add(callbackFunction, this);

Then, every time your player taps the screen, or clicks a mouse button, the callbackFunction is called, and will have the pointer object passed to it. This pointer object will have the x/y coordinates of the event in it. There are various x/ys to choose from, see here: http://docs.phaser.io/Phaser.Pointer.html

callbackFunction = function(pointer){    //Check where it happened and call function from here...}
Link to comment
Share on other sites

Thank you so much guys. I am using if (game.input.mousePointer.isDown) and if (game.input.mousePointer.clientX > game.world.width / 2) and it seems to work nice. What about the other question: if I compile the game with cocoonjs, will it work on the android app? Will it understand that mouse click is the click on the screen?

 

Also, I have fixed size pixels on the game, will it scale to fit the screen?

 

Thanks again!

Link to comment
Share on other sites

If you are not using callbacks, and don't mind your func1() and func2() potentially being called multiple times in an update loop, then use game.input.activePointer instead of game.input.mousePointer, because it will be referring to the last active pointer, whether it be a touch or a click.

Link to comment
Share on other sites

If you are not using callbacks, and don't mind your func1() and func2() potentially being called multiple times in an update loop, then use game.input.activePointer instead of game.input.mousePointer, because it will be referring to the last active pointer, whether it be a touch or a click.

Thank you! But what about the other questions? Do you maybe know the answer? Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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