Recommended Posts

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

Share this post


Link to post
Share on other sites

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);
			}
		}
	},

 

Share this post


Link to post
Share on other sites

* 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.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.