mak1ca Posted May 3, 2016 Share Posted May 3, 2016 Hello everyone, I just started Phaser, and I am trying to get a sprite to change on button click: create: function() { this.game.button = this.game.add.button(this.game.world.centerX-450, 660, 'button', this.nextPage(this.currentLevel, 0), this, 2, 1, 0); this.game.button.anchor.setTo(0.5); this.game.button.input.useHandCursor = true; this.game.button.input.priorityID = 0; this.game.sprite2 = this.game.add.sprite(this.game.world.centerX-475, this.game.world.centerY-20, 'button-comb'); this.game.sprite2.anchor.setTo(0.5); } Here is the nextPage code: nextPage: function(level, buttonNo) { if (buttonNo==0) { this.game.sprite2.visible=false; } } But every time I run this, it says 'Uncaught TypeError: Cannot set property 'visible' of undefined'. I have no idea what I am doing wrong. Please help me out! Much appreciated ____ MAK Quote Link to comment Share on other sites More sharing options...
zeke_chan Posted June 23, 2016 Share Posted June 23, 2016 Could you upload your full code somewhere to see? Hard to tell from this few lines. Here's what I'll assume: you are calling the nextPage function from another function called click (for example). If that's the case, your this keyword in the nextPage function no longer refers to the same this keyword in the create function. Are you using a global game variable? Meaning you declared it like so: var game = new Phaser.Game... If that's the case, try changing all the this.game to game in the create and nextPage functions. Not sure if that helps, if it doesn't, you have to share your source code to help you further. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.