JohnXu Posted June 29, 2018 Share Posted June 29, 2018 Thanks for you help. I am just a new learner of Phaser. and I use phaser-ce in my project. I add a sprite to a state, and I want to console something after the sprite is clicked down. what I use is "sprite.events.onInputDown.add(function(){console.log('something')},this)", but onInputDown seems to be no work. here is my code: import Phaser from 'phaser' export default class extends Phaser.State { constructor () { super() this.score = 0 this.highestScore = 0 } init () { } preload () { } create () { this.graphic = new Phaser.Graphics(this.game, 0, 0) this.graphic.beginFill(0xffee00) this.graphic.drawRoundedRect(0, 0, 200, 75, 5) this.graphic.endFill() this.textTexture = this.graphic.generateTexture() this.textSprite = this.add.sprite(this.game.width / 2, this.game.height / 2 + 100, this.textTexture) this.textSprite.anchor.setTo(0.5) this.text = new Phaser.Text(game, 0, 0, 'Try Again', { font: '40px', fill: '#ee5000', align: 'center' }) this.text.anchor.setTo(0.5) this.textSprite.addChild(this.text3) this.textSprite.inputEnabled = true //I want to click this sprite, but it seems didn't work, try button still cannot work this.textSprite.events.onInputDown.add(function () { console.log('tap dispatched') }, this) } update () { } render () { game.debug.spriteInfo(this.textSprite, 32, 32) game.debug.spriteInputInfo(this.textSprite, 32, 130) } } Here is some of my code, I want to change the state after getting the click signal. And I find the state.input is Ok. I had googled a long time, can't find the reason. Thanks for your help again. Link to comment Share on other sites More sharing options...
casey Posted June 30, 2018 Share Posted June 30, 2018 Did it work with a regular function (i.e. not a callback?) Link to comment Share on other sites More sharing options...
JohnXu Posted July 1, 2018 Author Share Posted July 1, 2018 11 hours ago, casey said: Did it work with a regular function (i.e. not a callback?) thanks. I may not understand what you mean. Do I have other way to handle the click events? Could you tell me ? and here is my github repositories: github repository you can find these code in Over.js. Thanks again. Link to comment Share on other sites More sharing options...
Yehuda Katz Posted July 1, 2018 Share Posted July 1, 2018 @JohnXu you set InputDown event correctly. Take in an account your comment, the console.log() event is also handled successfully. So the problem seems to be with game state change. Could you please show how do you do it? game.state.add('new_stage', NewStage); this.game.state.start('new_stage', true, false, some_custom_var); Please try to refer to game object via this.game instead of just game Waiting for your reply Link to comment Share on other sites More sharing options...
JohnXu Posted July 1, 2018 Author Share Posted July 1, 2018 1 hour ago, Yehuda Katz said: @JohnXu you set InputDown event correctly. Take in an account your comment, the console.log() event is also handled successfully. So the problem seems to be with game state change. Could you please show how do you do it? game.state.add('new_stage', NewStage); this.game.state.start('new_stage', true, false, some_custom_var); Please try to refer to game object via this.game instead of just game Waiting for your reply thanks you reply very much. here is my 'main.js' import 'pixi' import 'p2' import Phaser from 'phaser' import SplashState from './states/Splash' import GameState from './states/Game' import OverState from './states/Over' // console in mobile chrome import Eruda from 'eruda' Eruda.init() class Game extends Phaser.Game { constructor () { const docElement = document.documentElement const width = docElement.clientWidth const height = docElement.clientHeight super(width * 2, height * 2, Phaser.CANVAS, 'content') // width and height * 2 to solove not clear show in HD screen this.state.add('Splash', SplashState, false) this.state.add('Game', GameState, false) this.state.add('Over', OverState, false) this.state.start('Splash') } } splash state is a transitional picture. then go into Game state. After the game over, it goes into Over state. Part of the Game.js code is below: (for Game.js is large, you can see the whole code at my github: https://github.com/zixusir/rotationBalls.git) newBlock3.signal.addOnce(() => { console.log('GAME OVER') if (this.gameState) { this.gameState = false this.state.start('Over') Data.score = this.score localStorage.setItem('score', Data.score.toString()) if (this.score > this.hScore) { Data.hScore = this.score localStorage.setItem('hScore', this.hScore.toString()) } } }) Link to comment Share on other sites More sharing options...
Yehuda Katz Posted July 1, 2018 Share Posted July 1, 2018 @JohnXu could you please clarify, when you tap on textSprite, can you see console log message? this.textSprite.events.onInputDown.add(function () { console.log('tap dispatched') }, this) If you cannot see log message, can you see text sprite itself on screen? Link to comment Share on other sites More sharing options...
JohnXu Posted July 1, 2018 Author Share Posted July 1, 2018 48 minutes ago, Yehuda Katz said: @JohnXu could you please clarify, when you tap on textSprite, can you see console log message? this.textSprite.events.onInputDown.add(function () { console.log('tap dispatched') }, this) If you cannot see log message, can you see text sprite itself on screen? i can see the sprite on the screen, but cannot see the log mesage. Link to comment Share on other sites More sharing options...
Yehuda Katz Posted July 1, 2018 Share Posted July 1, 2018 @JohnXu then I assume you are using Phaser3, am I right? If that's true then it could be some bug, but lets debug it step by step. Please try this: this.game.input.onTap.add(function () { console.log('general tap'); }, this); Lets be sure that we are able to accept taps and there is no issues with input itself (as you can set how many pointers should be tracked by Phaser and bla bla bla) Link to comment Share on other sites More sharing options...
JohnXu Posted July 1, 2018 Author Share Posted July 1, 2018 1 minute ago, Yehuda Katz said: @JohnXu then I assume you are using Phaser3, am I right? If that's true then it could be some bug, but lets debug it step by step. Please try this: this.game.input.onTap.add(function () { console.log('general tap'); }, this); Lets be sure that we are able to accept taps and there is no issues with input itself (as you can set how many pointers should be tracked by Phaser and bla bla bla) thanks. but i use phaser-ce. and i have try game.input, it's ok. and in the debug info, i can see the inputInfo, which is listen by the game.input, but cannot see the spriteInputInfo. maybe i should try another pharser version. Link to comment Share on other sites More sharing options...
Yehuda Katz Posted July 1, 2018 Share Posted July 1, 2018 @JohnXu sure give a try to second version, I am using v2.6.2 for almost a year. It has bugs but I will never change it because I have no idea what new bugs are waiting me in new version ? BTW the only reason sprite may not get input is another sprite over it. Are you sure that you do not have any overlapping/transparent objects over that text sprite? Try to remove all code except the one which generates that sprite and then give a shot. To be sure that nothing else is interrupting main code. Link to comment Share on other sites More sharing options...
JohnXu Posted July 1, 2018 Author Share Posted July 1, 2018 8 minutes ago, Yehuda Katz said: @JohnXu sure give a try to second version, I am using v2.6.2 for almost a year. It has bugs but I will never change it because I have no idea what new bugs are waiting me in new version ? BTW the only reason sprite may not get input is another sprite over it. Are you sure that you do not have any overlapping/transparent objects over that text sprite? Try to remove all code except the one which generates that sprite and then give a shot. To be sure that nothing else is interrupting main code. ?resonable! I will have a try. Thank u again. Link to comment Share on other sites More sharing options...
JohnXu Posted July 9, 2018 Author Share Posted July 9, 2018 Thanks for all your help. And that i ve solved the problem. this issue due to i create a mew game object throughing extending from Phaser.Game. It turns out that this is unnecessary and can lead to problem. so i correct my code. and the sprite or btn touch events work thanks again. Link to comment Share on other sites More sharing options...
Recommended Posts