espace Posted December 2, 2017 Share Posted December 2, 2017 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; } }; Link to comment Share on other sites More sharing options...
espace Posted December 4, 2017 Author Share Posted December 4, 2017 nobody ? Link to comment Share on other sites More sharing options...
DanielKlava Posted December 4, 2017 Share Posted December 4, 2017 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: Player moves the cursor over the button; Button's frame is "1", as specified; Player clicks the button; 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! espace and 3man7 2 Link to comment Share on other sites More sharing options...
espace Posted December 4, 2017 Author Share Posted December 4, 2017 Thanks DanielKlava, i don't imagine that is possible to omit the spreadsheet at the begin. Link to comment Share on other sites More sharing options...
3man7 Posted December 5, 2017 Share Posted December 5, 2017 @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; }; DanielKlava and Fenopiù 1 1 Link to comment Share on other sites More sharing options...
Recommended Posts