apologies title i'm hoping explain why getting below error , how come changed fixed it.
overview:
i'm creating game have shoot 3 targets win. if hit 3 hit state loaded (consequently if don't hit 3 miss state loaded). once in hit state click screen triggers game logic determines if have won prize. game logic php script runs , returns win or lose variable via ajax. once returned state change triggered direct win state or lose state depending on result.
all 4 state code:
hit state
mygame.hit = function () {}; mygame.hit.prototype = { create: function () { this.bgd = this.add.image( 0, 0, 'hitbgd'); this.bgd.inputenabled = true; this.bgd.events.oninputdown.add( this.gamelogic, this); }, gamelogic: function () { var username = 'user'; if (typeof username !== 'undefined') { var request = new xmlhttprequest(); request.open('get', '_/api/logic.php?user=' + username, true); request.onreadystatechange = function () { if ((request.readystate === 4) && (request.status === 200)) { var returneddata = json.parse(request.responsetext); if (returneddata.outcome === true) { // winner mygame.game.state.start('won'); } else { // loser mygame.game.state.start('lose'); } } } request.send(); } else { mygame.game.state.start('lose'); } } }; miss state:
mygame.miss = function () {}; mygame.miss.prototype = { create: function (){ this.bgd = this.add.image( 0, 0, 'missbgd'); this.playagainbtn = this.add.button((this.game.width / 2) - 145, 235, 'playagainbtn', this.playagain, this); this.exitbtn = this.add.button((this.game.width / 2) + 145, 235, 'exitgamebtn', this.exitgame, this); }, playagain: function () { this.state.start('mainmenu'); }, exitgame: function () { alert('awaiting link client'); }, }; won state
mygame.won = function () {}; mygame.won.prototype = { create: function () { this.bgd = this.add.image( 0, 0, 'wonbgd'); this.playagain = this.add.button(130, 510, 'wonplayagainbtn', this.playagain, this); this.exitbtn = this.add.button(395, 510, 'wonexitgamebtn', this.exitgame, this); }, playagain: function () { this.state.start('mainmenu'); }, exitgame: function () { alert('awaiting link client'); } }; lose state
mygame.lose = function () {}; mygame.lose.prototype = { create: function (){ this.bgd = this.add.image( 0, 0, 'losebgd'); this.playagainbtn = this.add.button((this.game.width / 2) - 145, 235, 'playagainbtn', this.playagain, this); this.exitbtn = this.add.button((this.game.width / 2) + 145, 235, 'exitgamebtn', this.exitgame, this); }, playagain: function () { this.state.start('mainmenu'); }, exitgame: function () { alert('awaiting link client'); } }; as can see miss, won , lose states same apart in won state button called this.playagain instead of this.playagainbtn. first time win fine if play again without refreshing page , hit state, trigger game logic , win, below error
uncaught error: phaser.signal: listener required param of add() , should function. it points out this.playagain in won state.
i want know why changing this.playagainbtn removes error. should destroying events/signals? should naming functions differently though in different states/objects? why need call mygame.game.state.start('won'); in ajax request instead of this.state.start('won'); switch states?
when goes live , odds adjusted chance of happening ridiculously slim @ moment development winner.
any advice appreciated.
you add button called playagain callback method called playagain, have same name , causing error.
when mygame.won.create() called first time, , add mygame.won.playagain button reference original callback function mygame.won.playagain() retained button. second time start won-state mygame.won.create() called, , reference original callback function lost because overwritten button.
Comments
Post a Comment