hopgop1 Posted February 23, 2018 Share Posted February 23, 2018 Hi, I'm a relative noob to javascript and Phaser, and I'm having an issue coding my first proper game. I'm using Phaser version 2.6.2 and the latest version of Chrome browser. I'm asking the user a basic maths question, but when I draw the buttons for the user to click they appear but aren't clickable. Here is the console error: Quote Uncaught TypeError: this.onCreateCallback.call is not a function at Phaser.StateManager.loadComplete (phaser.js:29673) at Phaser.StateManager.preUpdate (phaser.js:29450) at Phaser.Game.updateLogic (phaser.js:35263) at Phaser.Game.update (phaser.js:35150) at Phaser.RequestAnimationFrame.updateRAF (phaser.js:60028) at _onLoop (phaser.js:60012) And here is the code I believe to be causing the error: game.add.button(50, 250 + i * 75, "buttons", this.checkAnswer).frame = i; And there is also this bit of code to draw the button mask: this.buttonMask = game.add.graphics(50, 250); this.buttonMask.beginFill(0xffffff); this.buttonMask.drawRect(0, 0, 400, 200); this.buttonMask.endFill(); numberTimer.mask = this.buttonMask; Any help would be appreciated, if you need any extra information to help me I'll gladly provide it. Been staring at this problem for over a week and I just can't figure out what's wrong, I hope it's something simple I'm doing wrong. Thanks Link to comment Share on other sites More sharing options...
Cronos72 Posted February 23, 2018 Share Posted February 23, 2018 Did you define this.check answer? And is this in the relevant context? Link to comment Share on other sites More sharing options...
hopgop1 Posted February 24, 2018 Author Share Posted February 24, 2018 Yeah I have a checkAnswer function, here's the code for it checkAnswer: function(button){ if(!this.isGameOver){ if(button.frame == this.randomSum){ this.score += Math.floor((this.buttonMask.x + 350) / 4); this.correctAnswers++; this.nextNumber(); } // wrong answer else{ if(this.correctAnswers > 1) { this.timeTween.stop(); } this.gameOver(button.frame + 1); } } }, Link to comment Share on other sites More sharing options...
mattstyles Posted February 28, 2018 Share Posted February 28, 2018 * Moved to Phaser forum as its Phaser specific about how to add elements to `game` * The button mask is irrelevant here, the error is still telling you (I think) that it doesn't know about the callback (this.checkAnswer). Whilst that function is defined there isn't enough info in your post to ascertain if it is actually in scope at the call site (game.add.button), try `console.log(this.checkAnswer)` in the line directly before the `game.add.button(...)` call. The `game.add.button(50, 250 + i * 75, "buttons", this.checkAnswer).frame = i` implies you have some looping logic, can you show the whole function? It definitely feels like a scoping issue. I'm also not sure that the add and then mutation works in one line, it probably does, but I'd instinctively think you should be doing the following: var button = game.add.button(50, 250 + i * 75, "buttons", this.checkAnswer) button.frame = i But I think this probably evaluates to the same anyway, and, in any case, the error is telling you the callback (I'm guessing this is the click handler) can't be called, which is most likely because `this.checkAnswer` is undefined, which could be a scoping issue caused by the loop I think you're using. Link to comment Share on other sites More sharing options...
samme Posted March 1, 2018 Share Posted March 1, 2018 See Link to comment Share on other sites More sharing options...
Recommended Posts