espace

button mute sound problem

Recommended Posts

hi,

I'm searching to have a mute sound button see my attachement.

The problem is that the frame of the button is disturbed by the mouse over it => so the button is always activated .

The desired behavior is if i click the sound is on mute and if i reclic the sound is activated. Do you have a tip for that.

 

//class button for mute sound
_button_stay=function(posx,posy,image){
	this.image=image;
	this.posx=posx;
	this.posy=posy;
	this.button=game.add.button(this.posx,this.posy,this.image,this.anim_on_click,this,1);
	
	this.button.visible=false;
	this.button.anchor.setTo(0.5,0.5);
	this.button.scale.setTo(0,0);
	this.flag=true;
	this.sound_click=game.add.audio('click');
};
_button_stay.prototype.audio_click = function() {
	this.sound_click.play();
};
_button_stay.prototype.show_button=function(){
	this.button.visible=true;
	this.tween_scale_button = game.add.tween(this.button.scale).to({x:1,y:1},500,Phaser.Easing.Bounce.Out,true,0);
};
_button_stay.prototype.anim_on_click=function(){
	if(this.button.frame==1){
		this.button.frame=0
		this.audio_click();
		music.pause();
		music_ambiance_mute();
		return true;
	}else{
		this.audio_click();
		music.resume();
		music_ambiance_activate();
		this.button.frame=1
		return true;
	}
};

 

button_sound.png

Share this post


Link to post
Share on other sites

Hello espace!

I think the issue is in the following line:

this.button=game.add.button(this.posx,this.posy,this.image,this.anim_on_click,this,1);

The sixth parameter defines which frame the button will be when you move the cursor over it. Since you are passing "1", Phaser will set the button's frame to 1 in a mouse over event.

As of now, the result logic is:

  1. Player moves the cursor over the button;
  2. Button's frame is "1", as specified;
  3. Player clicks the button;
  4. Function "anim_on_click" is called, and since the frame was set to "1", it will always enter in the first condition.

One simple solution would be to remove the last argument in your function call, I guess.

Also, one other way to do it is to, inside your "anim_on_click" function, try to control the state of the button with a boolean variable instead of checking it's frame number, but that's just a suggestion.

Hope it works!

Share this post


Link to post
Share on other sites

@DanielKlava answer is spot on. I don't see why it wouldn't work for you.

Credit goes to him for answer; I'll just provide the code if that's easier to understand.

1.

Delete the '1' at the end to make the default frame 0. (aka unslashed/unmuted sprite). (or change it to 0, same thing).

this.button = game.add.button(this.posx, this.posy, this.image, this.anim_on_click, this, 0);

Frames starts at 0; so you specified the button to go to the next frame (aka slashed/muted sprite) therefore the code skipped the first part.

2.

Use a variable instead of checking the current frame of the sprite.

var mutedYN = 0;

_button_stay.prototype.anim_on_click = function () {
    switch (mutedYN) {
        case 0:
            //mute
            this.audio_click();
            music.pause();
            music_ambiance_mute();
            this.button.frame = 1;
            break;
        case 1:
            //unmute
            this.audio_click();
            music.resume();
            music_ambiance_activate();
            this.button.frame = 0;
            break;
    }
    mutedYN = mutedYN ? 0 : 1;
};

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.