# Issue with button fading?

**URL:** <https://phaser.discourse.group/t/issue-with-button-fading/4834>\
**Category:** Phaser 3\
**Created:** [January 9, 2020, 6:09pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834 "2020-01-09T18:09:48Z")\
**Posts on this page:** 20\
**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:** [January 9, 2020, 6:09pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/1 "2020-01-09T18:09:48Z")

</div>

so i’m trying to fade out all of my menu buttons at the same time when clicking on 1 of the menu buttons, but for some reason it’s only fading out the one i clicked on when it should fade out all my buttons at the same time when clicked.

Here’s the direct link to project :

[https://thundros.github.io/menu-system-doosan/](https://thundros.github.io/menu-system-doosan/)

Here’s the direct link to `'Button.js'` :

[https://thundros.github.io/menu-system-doosan/src/Objects/Button.js](https://thundros.github.io/menu-system-doosan/src/Objects/Button.js)

Here’s the direct link to `'TitleScene.js'` :

[https://thundros.github.io/menu-system-doosan/src/Scenes/TitleScene.js](https://thundros.github.io/menu-system-doosan/src/Scenes/TitleScene.js)

Here’s the code{s} in `'TitleScene.js'` to create the button{s}, Line{s} `'85 - 106'` & `'198 - 220'` :

```javascript
CreateGameButton : function ( __objData ) {

	this. __objData =__ objData;
	this.__buttons = new Button ( );

	this.__buttons.CreateButton ({

		scene : this.__objData.scene, 
		add : this.__objData.add, 
		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, 

	});

	return this.__buttons;

}, 

```

in `'create ( )'` :

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

	// Game

	this.__button [this.__i] = this.CreateGameButton ({
		scene : this. __scene, add : this.add, 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.__buttonFadeInAlphaLevel [this.__i] = this.__fadeInMenu ( ).__buttonAlphaLevel [this.__i];
	this.__buttonFadeInAlphaDuration [this.__i] = this.__fadeInMenu ( ).__buttonAlphaDuration [this.__i];

	this.tweens.add ({
		targets : this.__button [this.__i], 
		alpha : this.__buttonFadeInAlphaLevel [this.__i], 
		duration : this.__buttonFadeInAlphaDuration [this.__i], 
	});

	this.__btnObjects.push ( this.__button [this.__i] );
		console.log ( this.__button [this.__i] );
	}

}

```

Here’s the code{s} in `'Button.js'` used to attempt to fade out all buttons :

In `'Button.js'`, Line{s} `'63 - 78'` :

```javascript
this.button.on ( 'pointerdown', function ( ) {
    this.__scene.tweens.add ({
        targets : [
            this.button, this.__text, 
        ], 
        targetScenes : this.__targetScene, 
        repeat : 0, 
        duration : 750, 
        alpha : { from : 1.0, to : 0.0 }, 
        easeType : 'Linear', 
        yoyo : false, 
        onComplete : ( ) => {
            this.__scene.scene.start ( this.__targetScene );
        }
    });
}.bind ( this ) );

```

Hopefully I explained correctly. If not, please do let me know & I will do my best to go into it in more depth.

Any help is as always, 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:** [January 9, 2020, 8:33pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/2 "2020-01-09T20:33:04Z")

</div>

You need to include all the buttons in tween `targets`.

Also, `Button` shouldn’t extend `Phaser.Scene`.

---

<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:** [January 9, 2020, 8:33pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/3 "2020-01-09T20:33:55Z")

</div>

@samme : but i thought i do that with `'this.button'` ?

---

<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:** [January 9, 2020, 8:35pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/4 "2020-01-09T20:35:32Z")

</div>

I think it’s just one.

```
this.button = this.__scene.add.sprite ( this.x, this.y, this.__key1 ).setInteractive ( );
```

---

<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:** [January 9, 2020, 8:36pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/5 "2020-01-09T20:36:21Z")

</div>

@samme : so what would i add?

---

<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:** [January 9, 2020, 8:39pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/6 "2020-01-09T20:39:14Z")

</div>

Maybe do it from TitleScene instead, using `targets: this.__btnObjects`.

---

<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:** [January 9, 2020, 9:07pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/7 "2020-01-09T21:07:37Z")

</div>

@samme : what am I doing wrong here? the buttons show up, but when I click on 1 of them, I get an error : `phaser.js:29829 Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined` :

```javascript
CreateGameButton : function ( __objData ) {

	this. __objData =__ objData;
	this.__buttons = new Button ( );

	this.__buttons.CreateButton ({

		scene : this.__objData.scene, 
		add : this.__objData.add, 
		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, 

	});

	return this.__buttons;

}, 

```

```javascript
this.__btnObjects = [];

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

	// Game

	this.__button [this.__i] = this.CreateGameButton ({
		scene : this. __scene, add : this.add, 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.__buttonFadeInAlphaLevel [this.__i] = this.__fadeInMenu ( ).__buttonAlphaLevel [this.__i];
	this.__buttonFadeInAlphaDuration [this.__i] = this.__fadeInMenu ( ).__buttonAlphaDuration [this.__i];

	this.tweens.add ({
		targets : this.__button [this.__i], 
		alpha : this.__buttonFadeInAlphaLevel [this.__i], 
		duration : this.__buttonFadeInAlphaDuration [this.__i], 
	});

	this.__btnObjects.push ( this.__button [this.__i] );
	console.log ( this.__button [this.__i] );

	this.__btnObjects [this.__i].button.on ( 'pointerdown', function ( ) {
		this.__scene.tweens.add ({
			targets : [
				this. __btnObjects, this.__ buttonText [this.__i], 
			], 
			targetScenes : this.__buttonTargetScene [this.__i], 
			repeat : 0, 
			duration : 750, 
			alpha : { from : 1.0, to : 0.0 }, 
			easeType : 'Linear', 
			yoyo : false, 
			onComplete : ( ) => {
				this.__scene.scene.start ( this.__buttonTargetScene );
			}
		});
	}.bind ( this ) );

}

```

---

<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:** [January 10, 2020, 12:31am UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/8 "2020-01-10T00:31:24Z")

</div>

Re. the tween config,

1. `targets` must be an array of objects to tween, so buttons only.
2. `targetScenes` isn’t a tweenable property of the targets, so remove it.
3. Fix the argument to `scene.start()`.

```javascript
this.__scene.tweens.add({
  targets: this.__btnObjects,
  repeat: 0,
  duration: 750,
  alpha: { from: 1.0, to: 0.0 },
  yoyo: false,
  onComplete: () => {
    this.__scene.scene.start(this.__buttonTargetScene[this.__i]);
  }
});

```

Stepping through your code will help you find a lot of these problems.

---

<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:** [January 10, 2020, 8:03pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/9 "2020-01-10T20:03:08Z")

</div>

@samme : now it’s not doing anything. It’s sitting there :

[https://thundros.github.io/menu-system-doosan/](https://thundros.github.io/menu-system-doosan/)

---

<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:** [January 10, 2020, 8:45pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/10 "2020-01-10T20:45:53Z")

</div>

![Screen Shot 2020-01-10](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/e/ecac08e045c261cd4d3597bf1e64e99e7cb220a8.png)

---

<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:** [January 10, 2020, 8:47pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/11 "2020-01-10T20:47:31Z")

</div>

Yes, but after that what does that mean? I don’t see what’s going on here.

---

<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:** [January 10, 2020, 9:38pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/12 "2020-01-10T21:38:39Z")

</div>

Look at the value of `this.__buttonTargetScene[this.__i]`, in **Watch Expressions**.

---

<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:** [January 10, 2020, 10:20pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/13 "2020-01-10T22:20:45Z")

</div>

@samme : But why? Why is it undefined?

---

<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:** [January 10, 2020, 10:22pm UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/14 "2020-01-10T22:22:02Z")

</div>

@samme : I define it above as :

```javascript
this.__buttonTargetScene = [

	'GameScene', 'OptionsScene', 'CreditsScene', 

];

```

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [January 11, 2020, 12:19am UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/15 "2020-01-11T00:19:13Z")

</div>

Hi @Thundros,  
If you look at the @samme screenshot:  
`this.__i == 3`  
Therefore:  
` this.__buttonTargetScene[3] == undefined`  
You are using this loop:  
`for ( this. __i = 0; this.__ i <= 2; this.__i++ ){}`  
`this.__i` reaches the value 3 because it is outside the block scope and the instruction **this.\_\_i++** is the last to execute in the loop.  
With **let** keyword you can add block scope:  
`for ( let i = 0; i <= 2; i++ ){}`  
Good luck.

---

<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:** [January 11, 2020, 12:20am UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/16 "2020-01-11T00:20:22Z")

</div>

@jjcapellan : are you saying it should be `'< 2'` instead of `'<= 2'` ?

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [January 11, 2020, 12:27am UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/17 "2020-01-11T00:27:59Z")

</div>

> [@jjcapellan](#):
>
> for ( let i = 0; i \<= 2; i++ ){}

You have to change your loop variable scope.

---

<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:** [January 11, 2020, 12:30am UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/18 "2020-01-11T00:30:06Z")

</div>

@jjcapellan : like this? :

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

	// Game

	this.__button [this.__i] = this.CreateGameButton ({
		scene : this. __scene, add : this.add, 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.__buttonFadeInAlphaLevel [this.__i] = this.__fadeInMenu ( ).__buttonAlphaLevel [this.__i];
	this.__buttonFadeInAlphaDuration [this.__i] = this.__fadeInMenu ( ).__buttonAlphaDuration [this.__i];

	this.__btnObjects.push ( this.__button [this.__i] );
	console.log ( this.__button [this.__i] );

	this.tweens.add ({
		targets : this.__btnObjects [this.__i], 
		alpha : this.__buttonFadeInAlphaLevel [this.__i], 
		duration : this.__buttonFadeInAlphaDuration [this.__i], 
	});

	this.__btnObjects [this.__i].button.on ( 'pointerdown', function ( ) {
		for ( this. __j = 0; this.__ j <= 2; this.__j++ ) {
			this.__scene.tweens.add ({
				targets : this.__btnObjects [this.__j], 
				repeat : 0, 
				duration : 750, 
				alpha : { from : 1.0, to : 0.0 }, 
				yoyo : false, 
				onComplete : ( ) => {
					this.scene.start (
						this.__buttonTargetScene, 
					);
				}
			});
		}
	}.bind ( this ) );

}

```

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [January 11, 2020, 12:37am UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/19 "2020-01-11T00:37:43Z")

</div>

No.  
I mean the variable you are using as a loop counter. If you use the keyword **this** (this.i), you are taking it out of the loop scope.  
The solution:

> [@jjcapellan](#):
>
> for ( let i = 0; i \<= 2; i++ ){}

---

<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:** [January 11, 2020, 12:46am UTC](https://phaser.discourse.group/t/issue-with-button-fading/4834/20 "2020-01-11T00:46:16Z")

</div>

@jjcapellan : so it’s working now, but why it is not fading all 3 buttons out when 1 is pressed? :

```javascript
for ( let __i = 0;__ i <= 2; __i++ ) {

	// Game

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

	this.__buttonFadeInAlphaLevel [__i] = this.__fadeInMenu ( ).__buttonAlphaLevel [__i];
	this.__buttonFadeInAlphaDuration [__i] = this.__fadeInMenu ( ).__buttonAlphaDuration [__i];

	this.__btnObjects.push ( this.__button [__i] );
	console.log ( this.__button [__i] );

	this.tweens.add ({
		targets : this.__btnObjects [__i], 
		alpha : this.__buttonFadeInAlphaLevel [__i], 
		duration : this.__buttonFadeInAlphaDuration [__i], 
	});

	this.__btnObjects [__i].button.on ( 'pointerdown', function ( ) {
		this.__scene.tweens.add ({
			targets : this.__btnObjects [__i], 
			repeat : 0, 
			duration : 750, 
			alpha : { from : 1.0, to : 0.0 }, 
			yoyo : false, 
			onComplete : ( ) => {
				this.scene.start (
					this.__buttonTargetScene [__i], 
				);
			}
		});
	}.bind ( this ) );

}

```

```javascript
this.__btnObjects [__i].button.on ( 'pointerdown', function ( ) {
	this.__scene.tweens.add ({
		targets : this.__btnObjects [__i], 
		repeat : 0, 
		duration : 750, 
		alpha : { from : 1.0, to : 0.0 }, 
		yoyo : false, 
		onComplete : ( ) => {
			this.scene.start (
				this.__buttonTargetScene [__i], 
			);
		}
	});
}.bind ( this ) );

```

[Next page](https://phaser.discourse.group/t/issue-with-button-fading/4834.md?page=2)
