Jump to content

Click On An Image not working


shahbazk30
 Share

Recommended Posts

Hi All,

 

I have recently started exploring phaser. I am struggling with the sprite.events.onInputDown event. Problem is that the click event is not firing after every click. It fires after clicking sprite multiple times.

 

I have even tried phaser example "02 - click on an image.js" which comes with the download but i am facing same issue with the example.

 

I have also tried same example from following link but problem is the same there too

http://gametest.mobi/phaser/examples/_site/view_full.html?d=basics&f=02+-+click+on+an+image.js&t=02%20-%20click%20on%20an%20image

 

I am testing on my PC (window 7) on chrome and firefox.

 

here is my code

 

window.onload = function () {
            var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create});
 
            function preload() {
                game.load.image('einstein', 'phaser.png');
            }
            
            var image;
            
            function create() {
                image = game.add.sprite(0, 0, 'einstein');
                image.inputEnabled = true;
                image.events.onInputDown.add(listener, this);
            }
            
            var n = 0;
            function listener() {
                alert('a');
                n++;
            }
        };
Link to comment
Share on other sites

Hhmm. not sure what is going on. I tried the link. When I click the first time it works.Then I click my mouse a second time (anywhere on or off the image) it doesn't work. Then I can click the image again. It seems as if the page looses focus and you have to click once to get focus back to the page then click the image again. Not sure if is something with alert boxes though.

Link to comment
Share on other sites

Hi Kobaltic,

 

the problem is not with the alert box. I have tried it after removing alert box but problem is still there. Thank you for the reply. However I think this (click event on sprite) is really basic stuff and it should work.

Link to comment
Share on other sites

Hi Magnifisite,

 

I have also tried 

 

image.inputEnabled = true;
image.events.onInputDown.add(function(){alert('working');});

 

with Phaser 1.1.5 and it doesn't work properly after first click. Please try clicking multiple times and see what happens. 

 

Thanks

Link to comment
Share on other sites

I'm not sure if game.input.onDown.add is working or if it is just as broken as onInputDown on a sprite itself but you could create your own onInputDown with game.input.onDown.add.

I will try to make it, will come back with results!

Link to comment
Share on other sites

I have tested game.input.onDown.add with both phaser1.5 and phaser1.4. Unfortunately I found same problem with this too. However i find the problem is less frequent with game.input.onDown :( .

 

I am a newbie in Html5 game dev and was thinking to use Phaser.js for developing my game as it looked really promising. But after experimenting with Phaser for last few days I think it is rich but unstable for game development as some basic stuff still have bugs. I hope things will change in near future :). Till then i should stick with PIXI.js. 

 

Thank you Magnifisite for your response i really appreciate that.

Link to comment
Share on other sites

The issue with the Example is the use of the alert box, nothing else (clicking images works fine in Phaser!).

 

Remove the alert() and put a console.log in instead, you'll notice it fires every single time without failure.

 

I have updated the basic example to not use alert any more on the back of this thread. The problem with alert is that most browsers will lock down from processing the same function for a short period of time after an alert has been created, that is why if you click, get the alert, then click again it will miss most of those clicks for a few seconds, and then it will seem to start working again - at least until the next alert appears, dismiss that and the browser will lock-down for a few seconds again.

 

Thanks for pointing this out though :) I'll make sure "alert" isn't used anywhere in any example from now on.

Link to comment
Share on other sites

Hi Admin,

 

I have tested Phaser example without displaying alert box and found that example is working fine. As indicated above in thread i had tested it before without alert box and it was not working then, I cant remember what i did wrong then.

 

Thank you for figuring this out, Though it is late for me as i have just completed my first game with PIXI.js :)

Hope this thread will prove beneficial for some other newbie.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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