# Problem with Buttons?

**URL:** <https://phaser.discourse.group/t/problem-with-buttons/4474>\
**Category:** Phaser 3\
**Created:** [December 5, 2019, 6:33pm UTC](https://phaser.discourse.group/t/problem-with-buttons/4474 "2019-12-05T18:33:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [December 5, 2019, 6:33pm UTC](https://phaser.discourse.group/t/problem-with-buttons/4474/1 "2019-12-05T18:33:43Z")

</div>

So I have this `TitleScene.js` :

```javascript
class TitleScene extends Phaser.Scene {

	constructor ( ) {
		super ( 'Title' );
	}

	this.__scene = this;

	this.__button = [];

	this.__buttonFadeInAlphaLevel = [];
	this.__buttonFadeInAlphaDuration = [];

	this.__buttonX = [

		( config.width / 2 ), 
		( config.width / 2 ), 
		( config.width / 2 ), 

	];

	this.__buttonY = [

		( ( config.height / 2 ) - 100 ), 
		( ( config.height / 2 ) ), 
		( ( config.height / 2 ) + 100 )

	];

	this.__buttonKeys = [

		'blueButton1', 'blueButton2', 

	];

	this.__buttonText = [

		'Play', 'Options', 'Credits', 

	];

	this.__buttonTargetScene = [

		'Game', 'Options', 'Credits', 

	];

	this.__buttonLocked = [

		true, true, true, 

	];

	CreateGameButton ( __objData ) {

		this. __objData =__ objData;

		this.__buttonObj = {

			scene : this.__objData.scene, 
			x : this.__objData.x, 
			y : this.__objData.y, 
			key1 : this.__objData.key1, 
			key2 : this.__objData.key2, 
			text : this.__objData.text, 
			targetScene : this.__objData.targetScene, 
			locked : this.__objData.locked, 

		}

		this.__buttons = new Button ( this.__buttonObj.scene, {

			x : this. __buttonObj.x, y : this.__ buttonObj.y, 
			key1 : this. __buttonObj.key1, key2 : this.__ buttonObj.key2, text : this.__buttonObj.text, 
			locked : this.__buttonObj.locked, 

		} );

		return this.__buttons;

	}

	for ( this. __i = 0; this.__ i <= 2; this.__i++ ) {

		this.__button [this.__i] = this.CreateGameButton ({
			scene : this. __scene, x : this.__ buttonX [this.__i], y : this.__buttonY [this.__i], 
			key1 : this.__buttonKeys [0], key2 : this.__buttonKeys [1], text : this.__buttonText [this.__i], 
			targetScene : this.__buttonTargetScene [this.__i], locked : this.__buttonLocked [this.__i], 
		});

	}

}

```

& this `working` `Button.js` :

```javascript
class Button extends Phaser.GameObjects.Container {

	constructor ( scene, __objData ) {

		super ( scene, __objData );

		this. __objData =__ objData;

		this.__scene = scene;
		this. __x = this.__ objData.x;
		this. __y = this.__ objData.y;
		this. __key1 = this.__ objData.key1;
		this. __key2 = this.__ objData.key2;
		this. __bText = this.__ objData.text;
		this. __buttonLocked = this.__ objData.locked;
		this. __targetScene = this.__ objData.targetScene;

		this.__scene = scene;
		this.x = this.__x;
		this.y = this.__y;

		this.button = this.__scene.add.sprite ( 0, 0, this.__key1 ).setInteractive ( );

		this. __text = this.__ scene.add.text ( 0, 0, this.__bText, {
			fontSize : '32px', 
			fill : '#fff', 
		} );

		Phaser.Display.Align.In.Center ( this.__text, this.button );

		this.add ( this.button );
		this.add ( this.__text );

		this.button.on ( 'pointerdown', function ( ) {
			if ( this.__buttonLocked === false ) {
				this.__scene.scene.start ( this.targetScene );
			}
		}.bind ( this ) );

		this.button.on ( 'pointerover', function ( ) {
			if ( this.__buttonLocked === false ) {
				this.button.setTexture ( this.__key2 );
			}
		}.bind ( this ) );

		this.button.on ( 'pointerout', function ( ) {
			if ( this.__buttonLocked === false ) {
				this.button.setTexture ( this.__key1 );
			}
		}.bind ( this ) );

		this.__scene.add.existing ( this );

	}

}

```

& this `non-working` `Button.js` :

```javascript
class Button extends Phaser.GameObjects.Container {

	constructor ( __objData ) {

		super ( __objData );

		this. __objData =__ objData;

		this. __scene = this.__ objData.scene;
		this. __x = this.__ objData.x;
		this. __y = this.__ objData.y;
		this. __key1 = this.__ objData.key1;
		this. __key2 = this.__ objData.key2;
		this. __bText = this.__ objData.text;
		this. __buttonLocked = this.__ objData.locked;
		this. __targetScene = this.__ objData.targetScene;

		this.scene = this.__scene;
		this.x = this.__x;
		this.y = this.__y;

		this.button = this.__scene.add.sprite ( 0, 0, this.__key1 ).setInteractive ( );

		this. __text = this.__ scene.add.text ( 0, 0, this.__bText, {
			fontSize : '32px', 
			fill : '#fff', 
		} );

		Phaser.Display.Align.In.Center ( this.__text, this.button );

		this.add ( this.button );
		this.add ( this.__text );

		this.button.on ( 'pointerdown', function ( ) {
			if ( this.__buttonLocked === false ) {
				this.__scene.scene.start ( this.targetScene );
			}
		}.bind ( this ) );

		this.button.on ( 'pointerover', function ( ) {
			if ( this.__buttonLocked === false ) {
				this.button.setTexture ( this.__key2 );
			}
		}.bind ( this ) );

		this.button.on ( 'pointerout', function ( ) {
			if ( this.__buttonLocked === false ) {
				this.button.setTexture ( this.__key1 );
			}
		}.bind ( this ) );

		this.__scene.add.existing ( this );

	}

}

```

I’m trying to fix this issue that is not allowing me to use :

```javascript
class Button extends Phaser.GameObjects.Container {
    constructor ( __objData ) {
        super ( __objData );
        this. __objData =__ objData;
        this. __scene = this.__ objData.scene;
    });
...

```

instead of :

```javascript
class Button extends Phaser.GameObjects.Container {
    constructor ( scene, __objData ) {
        super ( scene, __objData );
        this. __objData =__ objData;
        this.__scene = scene;
    });
...

```

The problem is `scene` & `this.__objData.scene` are both the same but for some reason I’m getting the error `phaser.js:2862 Uncaught TypeError: Cannot read property 'queueDepthSort' of undefined`  
when calling :

```javascript
this.__buttons = new Button ({
    scene : this. __buttonObj.scene, x : this.__ buttonObj.x, y : this.__buttonObj.y, 
    key1 : this. __buttonObj.key1, key2 : this.__ buttonObj.key2, text : this.__buttonObj.text, 
    locked : this.__buttonObj.locked, 
});

```

as opposed to calling :

```javascript
this.__buttons = new Button ( this.__buttonObj.scene, {
    x : this. __buttonObj.x, y : this.__ buttonObj.y, 
    key1 : this. __buttonObj.key1, key2 : this.__ buttonObj.key2, text : this.__buttonObj.text, 
    locked : this.__buttonObj.locked, 
} );

```

Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [December 5, 2019, 8:41pm UTC](https://phaser.discourse.group/t/problem-with-buttons/4474/2 "2019-12-05T20:41:50Z")

</div>

`super` is [Container’s constructor](https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Container.html#Container) so the arguments must match

```
(scene, x?, y?, children?)
```

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [December 5, 2019, 8:43pm UTC](https://phaser.discourse.group/t/problem-with-buttons/4474/3 "2019-12-05T20:43:15Z")

</div>

@samme : Ok, so I just tried that & it worked. Problem is, I used to be able to click on the buttons & it would go to another scene. But right now when I click them, they disappear then reappear.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [December 5, 2019, 8:53pm UTC](https://phaser.discourse.group/t/problem-with-buttons/4474/4 "2019-12-05T20:53:34Z")

</div>

Try [phaser-plugin-scene-watcher](https://github.com/samme/phaser-plugin-scene-watcher), it can help you figure out what the scenes are doing at least.

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [December 5, 2019, 9:20pm UTC](https://phaser.discourse.group/t/problem-with-buttons/4474/5 "2019-12-05T21:20:54Z")

</div>

@samme : does this work with 3.21.0? If not, how can I make it work with 3.21.0 ?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [December 5, 2019, 10:39pm UTC](https://phaser.discourse.group/t/problem-with-buttons/4474/6 "2019-12-05T22:39:12Z")

</div>

It should.
