super_frog Posted May 21, 2015 Share Posted May 21, 2015 I am trying to add click event to a text (game.add.text), but it is not working for me.I've seen an example and tried to implement, but I get an error saying 'Cannot read property 'onInputDown' of undefined'. The code is: var btn = game.add.text(game.world.centerX, game.world.centerY, 'blah1'); btn.inputEnabled = true; btn.events.onInputDown.add(function (){alert("blah2");}, this); The code is place in: PlayGround.gameOver = { create: function () { // here is placed the above code } } What am I doing wrong? Link to comment Share on other sites More sharing options...
super_frog Posted May 22, 2015 Author Share Posted May 22, 2015 I found out the problem. I didn't copy the code that didn't work, I wrote it from memory and left out the culprit.The above code works and mine doesn't because at the end of it I placed anchor.set(.5) and that returned me Point object instead of Text object. (how silly of me ) William Carswell 1 Link to comment Share on other sites More sharing options...
William Carswell Posted May 22, 2015 Share Posted May 22, 2015 Hi,Do you have the example available?I wonder if you change game to this.game it would solve your undefined issue. Link to comment Share on other sites More sharing options...
super_frog Posted May 22, 2015 Author Share Posted May 22, 2015 Hi, Here's example codevar btn = game.add.text(game.world.centerX, game.world.centerY, 'PLAY', {font: '40px Impact', fill: 'black', align: 'center'}).anchor.set(.5);btn.inputEnabled = true;btn.events.onInputDown.add(play, this);I've tried with this.game, but the same error occurs. Link to comment Share on other sites More sharing options...
androo Posted February 5, 2016 Share Posted February 5, 2016 hi. i don't know if you already solved it but i think you need to add 'this.' before play. example: btn.events.onInputDown.add(this.play, this); try this. That should work. Link to comment Share on other sites More sharing options...
Recommended Posts