megamaster Posted February 23, 2017 Share Posted February 23, 2017 Hi fellow phasers, I am having difficulty making a single event occur when pointer input is pressed down. I've searched the forums for a solution but nothing seems to be working for me. The recommendation that I have seen is this code: game.input.onDown.add(doSomething, this); function doSomething() { // will only ever be called once, when the the input is down } I simply want to reduce health of a character when the sprite is clicked. This is what I have written: var player1; var health1 = 100; function create() { player1 = game.add.sprite(44, 70, 'stand'); health1Text = game.add.text(2, 10, 'health: 100', { fontSize: '10px Arial', fill: '#fff' }); player1.inputEnabled = true; player1.onDown.add(update, this); function update() { health1 += -10; health1Text.text = 'health: ' + health1; What happens when I run it is that the health constantly decreases without any input pressed. I have tried different input methods as well, but they won't reduce the health by a factor of 10 at a time. Also the font size is not working for me either. Thanks for any and all help! Link to comment Share on other sites More sharing options...
samme Posted February 23, 2017 Share Posted February 23, 2017 var player1; var health1 = 100; var health1Text = undefined; function create() { player1 = game.add.sprite(44, 70, 'stand'); health1Text = game.add.text(2, 10, 'health: 100', { font: '10px Arial', fill: '#fff' }); player1.inputEnabled = true; player1.onDown.add(decreaseHealth, this); } function update() { // update things… } function decreaseHealth() { health1 += -10; health1Text.text = 'health: ' + health1; } fontSize should be font update is probably your state update function; you need a separate callback for the input event you need to declare health1Text in the outer scope if you want to use it in multiple functions check your browser console for error messages Link to comment Share on other sites More sharing options...
megamaster Posted February 24, 2017 Author Share Posted February 24, 2017 Thanks for the feedback! I've got the font working now, but I'm still having a bit of trouble with the on-click event. I made the health a separate function but it is still not working. Any suggestions? Link to comment Share on other sites More sharing options...
samme Posted February 24, 2017 Share Posted February 24, 2017 Should be player1.events.onInputDown.add(decreaseHealth, this); Check the browser console for errors! Link to comment Share on other sites More sharing options...
megamaster Posted February 26, 2017 Author Share Posted February 26, 2017 So here's the error I got using google's console: Uncaught TypeError: Cannot read property 'add' of undefined at Object.create (part2.html:41) at c.StateManager.loadComplete (phaser.min.js:8) at c.SignalBinding.execute (phaser.min.js:8) at c.Signal.dispatch (phaser.min.js:8) at c.Loader.nextFile (phaser.min.js:17) at c.Loader.fileComplete (phaser.min.js:17) at HTMLImageElement.a.data.onload (phaser.min.js:17) any suggestions? Link to comment Share on other sites More sharing options...
megamaster Posted February 26, 2017 Author Share Posted February 26, 2017 So I figured out the issue. This: player1.onInputDown.add(decreaseHealth, this); Should be this: player1.events.onInputDown.add(decreaseHealth, this); Thanks for reminding about the console. It helped me spot a few other errors as well Link to comment Share on other sites More sharing options...
Recommended Posts