Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php on line 101

Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks/edd-blocks.php on line 101

Warning: The magic method GAINWP_Manager::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/gainwp.php on line 78

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 555

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 585

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 617

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 651

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 686

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 745

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 785

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":57,"date":"2017-05-17T21:46:26","date_gmt":"2017-05-17T21:46:26","guid":{"rendered":"http:\/\/phasergames.com\/uncategorized\/phaser-game-input-onup-event-firing-twice\/"},"modified":"2017-05-17T22:07:51","modified_gmt":"2017-05-17T22:07:51","slug":"phaser-game-input-onup-event-firing-twice","status":"publish","type":"post","link":"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/","title":{"rendered":"Phaser game.input.onUp event firing twice!"},"content":{"rendered":"

Yesterday I was making a game for an online course I\u2019m writing. The game has a flying cat and when you click anywhere on the canvas the cat will change position. This is the line of code I was using:<\/p>\n

game.input.onUp.add(this.changePos, this);<\/p>\n

The game was working fine on desktop mode, but when I changed to the mobile simulator on Google chrome it looked like the event did not fire at all because the cat didn\u2019t move. After about 15 minutes of trying to debug, I found that instead of not firing the event was actually firing twice. What was happening was that the browser was detecting two different events.<\/p>\n

A touch event for the device
\nA mouse event from the browser<\/p>\n

If I had simply tested the game on mobile, it would have worked fine, because the mobile device would not have dispatched a mouse event. So all in all, the game would be fine, but I still needed to test it on chrome, so I needed a workaround. Here\u2019s what I did.<\/p>\n

When you use the line of code above, it passes a parameter to the function which is called a pointer. The documentation for Phaser defines a pointer this way.<\/p>\n

A Pointer object is used by the Mouse, Touch and MSPoint managers and represents a single finger on the touch screen.<\/p>\n

The pointer contains a lot of information including if the user is using a mouse or not. I could simply return from the function if the pointer.isMouse==true, but I do not want to have to maintain two sets of code, and want my code to work on both desktop and mobile.<\/p>\n

So the solution is twofold<\/p>\n

1. Detect if the code is being run on a mobile device.
\n2. Return from the function if a mouse event is detected and we are using a mobile device.<\/p>\n

The first part I am already using in the game.<\/p>\n

isMobile=navigator.userAgent.indexOf(\u201cMobile\u201d);
\nisMobile=(isMobile==-1)?false:true;<\/p>\n

Now Phaser does have built in mobile detection, but I use this code to set things up before the game is declared, so I can set the game size.<\/p>\n

The second part I simply include a pointer parameter variable in my function and check if is a mouse, and if we are using a device, return.<\/p>\n

Here is what the code looks like:<\/p>\n

Main.js
\nvar game;
\nwindow.onload = function()
\n{
\nisMobile=navigator.userAgent.indexOf(\"Mobile\");
\nisMobile=(isMobile==-1)?false:true;<\/code><\/p>\n

if (isMobile==false)
\n{
\ngame=new Phaser.Game(480,640,Phaser.AUTO,”ph_game”);
\n}
\nelse
\n{
\ngame=new Phaser.Game(window.innerWidth,window.innerHeight,Phaser.AUTO,”ph_game”);
\nconsole.log(“Mobile”);
\n}<\/p>\n

game.state.add(“StateMain”,StateMain);
\ngame.state.start(“StateMain”);
\n}<\/p>\n

StateMain.js
\n
\nvar StateMain = {<\/code><\/p>\n

preload: function () {<\/p>\n

},<\/p>\n

create: function () {
\ngame.input.onUp.add(this.changePos, this);
\n},
\nchangePos:function(p)
\n{
\nif (p.isMouse==true && isMobile==true)
\n{
\nconsole.log(“caught mouse!”);
\nreturn;
\n}<\/strong>
\nconsole.log(“clicked”);
\n},
\nupdate: function () {<\/p>\n

}<\/p>\n

}<\/p>\n

This allows us to keep testing on chrome while avoiding this pesky annoyance.<\/p>\n

I hope this helps someone.<\/p>\n

Thanks!<\/p>\n

UPDATE!::<\/p>\n

I found that sometimes this method isn\u2019t working.<\/p>\n

An alternative would be to check the id of the pointer and if it isn\u2019t equal to one, ignore it.<\/p>\n

if (pointer.id!=1)
\n{
\nreturn;
\n}<\/p>\n","protected":false},"excerpt":{"rendered":"

Yesterday I was making a game for an online course I\u2019m writing. The game has a flying cat and when you click anywhere on the canvas the cat will change position. This is the line of code I was using: game.input.onUp.add(this.changePos, this); The game was working fine on desktop mode, but when I changed to […]<\/p>\n","protected":false},"author":1,"featured_media":82,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[33],"tags":[24,25,23,7],"class_list":["post-57","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bugs","tag-event","tag-firing","tag-gameinputonup","tag-phaser"],"yoast_head":"\nPhaser game.input.onUp event firing twice! - Phaser Games<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Phaser game.input.onUp event firing twice! - Phaser Games\" \/>\n<meta property=\"og:description\" content=\"Yesterday I was making a game for an online course I\u2019m writing. The game has a flying cat and when you click anywhere on the canvas the cat will change position. This is the line of code I was using: game.input.onUp.add(this.changePos, this); The game was working fine on desktop mode, but when I changed to […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/\" \/>\n<meta property=\"og:site_name\" content=\"Phaser Games\" \/>\n<meta property=\"article:published_time\" content=\"2017-05-17T21:46:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-05-17T22:07:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/media.publit.io\/file\/w_1667,h_1537,c_fit,q_80\/websiteImages\/phasergames\/06-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1667\" \/>\n\t<meta property=\"og:image:height\" content=\"1537\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"William\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"William\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/\",\"url\":\"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/\",\"name\":\"Phaser game.input.onUp event firing twice! - Phaser Games\",\"isPartOf\":{\"@id\":\"https:\/\/phasergames.com\/#website\"},\"datePublished\":\"2017-05-17T21:46:26+00:00\",\"dateModified\":\"2017-05-17T22:07:51+00:00\",\"author\":{\"@id\":\"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951\"},\"breadcrumb\":{\"@id\":\"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/phasergames.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Phaser game.input.onUp event firing twice!\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/phasergames.com\/#website\",\"url\":\"https:\/\/phasergames.com\/\",\"name\":\"Phaser Games\",\"description\":\"Tutorials Code and Resources\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/phasergames.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951\",\"name\":\"William\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\/\/phasergames.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g\",\"caption\":\"William\"},\"url\":\"https:\/\/phasergames.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Phaser game.input.onUp event firing twice! - Phaser Games","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/","og_locale":"en_US","og_type":"article","og_title":"Phaser game.input.onUp event firing twice! - Phaser Games","og_description":"Yesterday I was making a game for an online course I\u2019m writing. The game has a flying cat and when you click anywhere on the canvas the cat will change position. This is the line of code I was using: game.input.onUp.add(this.changePos, this); The game was working fine on desktop mode, but when I changed to […]","og_url":"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/","og_site_name":"Phaser Games","article_published_time":"2017-05-17T21:46:26+00:00","article_modified_time":"2017-05-17T22:07:51+00:00","og_image":[{"width":1667,"height":1537,"url":"https:\/\/media.publit.io\/file\/w_1667,h_1537,c_fit,q_80\/websiteImages\/phasergames\/06-01.jpg","type":"image\/jpeg"}],"author":"William","twitter_card":"summary_large_image","twitter_misc":{"Written by":"William","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/","url":"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/","name":"Phaser game.input.onUp event firing twice! - Phaser Games","isPartOf":{"@id":"https:\/\/phasergames.com\/#website"},"datePublished":"2017-05-17T21:46:26+00:00","dateModified":"2017-05-17T22:07:51+00:00","author":{"@id":"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951"},"breadcrumb":{"@id":"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/phasergames.com\/phaser-game-input-onup-event-firing-twice\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/phasergames.com\/"},{"@type":"ListItem","position":2,"name":"Phaser game.input.onUp event firing twice!"}]},{"@type":"WebSite","@id":"https:\/\/phasergames.com\/#website","url":"https:\/\/phasergames.com\/","name":"Phaser Games","description":"Tutorials Code and Resources","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/phasergames.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en"},{"@type":"Person","@id":"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951","name":"William","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/phasergames.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g","caption":"William"},"url":"https:\/\/phasergames.com\/author\/admin\/"}]}},"featured_image_src":"https:\/\/media.publit.io\/file\/w_600,h_400,c_fill,q_80\/websiteImages\/phasergames\/06-01.jpg","featured_image_src_square":"https:\/\/media.publit.io\/file\/w_600,h_600,c_fill,q_80\/websiteImages\/phasergames\/06-01.jpg","author_info":{"display_name":"William","author_link":"https:\/\/phasergames.com\/author\/admin\/"},"_links":{"self":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts\/57"}],"collection":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":1,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts\/57\/revisions\/83"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/media\/82"}],"wp:attachment":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}