Skip to content

Commit c037483

Browse files
authored
Merge pull request jvondermarck#108 from Thompson6626/Fixingmusicandsoundbutton
Fixed the music button on the main menu not working
2 parents 57c7e8a + 14e54ed commit c037483

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

src/main/java/com/dinosaur/dinosaurexploder/view/DinosaurMenu.java

+38-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.almasb.fxgl.app.scene.FXGLMenu;
44
import com.almasb.fxgl.app.scene.MenuType;
55
import com.almasb.fxgl.dsl.FXGL;
6+
import com.almasb.fxgl.scene.Scene;
67
import com.almasb.fxgl.ui.FontType;
78
import com.dinosaur.dinosaurexploder.model.GameConstants;
89
import javafx.scene.control.Button;
@@ -24,12 +25,13 @@
2425
import java.io.FileNotFoundException;
2526

2627
public class DinosaurMenu extends FXGLMenu {
28+
private MediaPlayer mainMenuSound;
2729

2830
public DinosaurMenu() {
2931
super(MenuType.MAIN_MENU);
3032

3133
Media media = new Media(getClass().getResource(GameConstants.MAINMENU_SOUND).toExternalForm());
32-
MediaPlayer mainMenuSound = new MediaPlayer(media);
34+
mainMenuSound = new MediaPlayer(media);
3335
mainMenuSound.play();
3436
mainMenuSound.setCycleCount(MediaPlayer.INDEFINITE);
3537

@@ -38,7 +40,7 @@ public DinosaurMenu() {
3840
var title = FXGL.getUIFactoryService().newText(GameConstants.GAME_NAME, Color.LIME, FontType.MONO, 35);
3941
var startButton = new Button("Start Game");
4042
var quitButton = new Button("Quit");
41-
43+
4244
Slider volumeSlider = new Slider(0, 1, 1);
4345
volumeSlider.setShowTickLabels(true);
4446
volumeSlider.setShowTickMarks(true);
@@ -53,13 +55,14 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
5355
volumeLabel.setText(String.format("Volume: %.0f%%", newValue.doubleValue() * 100));
5456
}
5557
});
56-
57-
58+
59+
5860
try {
5961

6062

6163
FileInputStream fileInputStream = new FileInputStream("../dinosaur-exploder/src/main/resources/assets/textures/dinomenu.png");
6264
FileInputStream mutemusic_button = new FileInputStream("../dinosaur-exploder/src/main/resources/assets/textures/silent.png");
65+
FileInputStream audioOnButton = new FileInputStream("../dinosaur-exploder/src/main/resources/assets/textures/playing.png");
6366

6467
// image for dino in main menu
6568
Image image = new Image(fileInputStream);
@@ -71,16 +74,21 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
7174
imageView.setPreserveRatio(true);
7275

7376
//adding image to manually mute music
74-
7577
Image mute = new Image(mutemusic_button);
76-
ImageView imageView_mute = new ImageView(mute);
77-
imageView_mute.setFitHeight(40);
78-
imageView_mute.setFitWidth(50);
79-
imageView_mute.setX(490);
80-
imageView_mute.setY(20);
81-
imageView_mute.setPreserveRatio(true);
78+
79+
80+
Image audioOn = new Image(audioOnButton);
81+
ImageView imageViewPlaying = new ImageView(audioOn);
82+
imageViewPlaying.setFitHeight(40);
83+
imageViewPlaying.setFitWidth(50);
84+
imageViewPlaying.setX(490);
85+
imageViewPlaying.setY(20);
86+
imageViewPlaying.setPreserveRatio(true);
87+
8288

8389
startButton.setMinSize(50, 50);
90+
startButton.setPrefSize(140,60);
91+
8492
quitButton.setMinSize(140, 50);
8593

8694
title.setTranslateY(100);
@@ -93,7 +101,7 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
93101
quitButton.setTranslateY(500);
94102
quitButton.setTranslateX(getAppWidth() / 2 - 50);
95103
quitButton.setStyle("-fx-font-size:20");
96-
104+
97105
BorderPane root = new BorderPane();
98106
root.setTop(title);
99107
BorderPane.setAlignment(title, Pos.CENTER);
@@ -113,22 +121,31 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
113121
mainMenuSound.stop();
114122
});
115123

116-
imageView_mute.setOnMouseClicked(event -> {
117-
if (mainMenuSound.getStatus() == MediaPlayer.Status.PLAYING) {
118-
mainMenuSound.pause();
119-
} else {
120-
mainMenuSound.play();
121-
}
124+
imageViewPlaying.setOnMouseClicked(mouseEvent -> {
125+
if (mainMenuSound.isMute()){
126+
mainMenuSound.setMute(false);
127+
imageViewPlaying.setImage(audioOn);
128+
} else {
129+
mainMenuSound.setMute(true);
130+
imageViewPlaying.setImage(mute);
131+
}
122132
});
133+
123134
quitButton.setOnAction(event -> fireExit());
124135

125136
getContentRoot().getChildren().addAll(
126-
bg, title, startButton, quitButton, imageView, imageView_mute,volumeSlider, volumeLabel
137+
bg, title, startButton, quitButton, imageView, imageViewPlaying, volumeSlider, volumeLabel
127138
);
128139
}
129140
catch (FileNotFoundException e){
130-
System.out.println("File not found" + e.getMessage());
141+
System.out.println("File not found" + e.getMessage());
131142
}
132143
}
144+
@Override
145+
public void onEnteredFrom(Scene prevState) {
146+
super.onEnteredFrom(prevState);
147+
FXGL.getAudioPlayer().stopAllSounds();
148+
mainMenuSound.play();
149+
}
133150

134-
}
151+
}
19.1 KB
Loading

0 commit comments

Comments
 (0)