Jump to content

Scope of an object in a different function


mak1ca
 Share

Recommended Posts

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

Link to comment
Share on other sites

  • 1 month later...

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...