Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the music button on the main menu not working #108

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions src/main/java/com/dinosaur/dinosaurexploder/view/DinosaurMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.almasb.fxgl.app.scene.FXGLMenu;
import com.almasb.fxgl.app.scene.MenuType;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.scene.Scene;
import com.almasb.fxgl.ui.FontType;
import com.dinosaur.dinosaurexploder.model.GameConstants;
import javafx.scene.control.Button;
Expand All @@ -24,12 +25,13 @@
import java.io.FileNotFoundException;

public class DinosaurMenu extends FXGLMenu {
private MediaPlayer mainMenuSound;

public DinosaurMenu() {
super(MenuType.MAIN_MENU);

Media media = new Media(getClass().getResource(GameConstants.MAINMENU_SOUND).toExternalForm());
MediaPlayer mainMenuSound = new MediaPlayer(media);
mainMenuSound = new MediaPlayer(media);
mainMenuSound.play();
mainMenuSound.setCycleCount(MediaPlayer.INDEFINITE);

Expand All @@ -38,7 +40,7 @@ public DinosaurMenu() {
var title = FXGL.getUIFactoryService().newText(GameConstants.GAME_NAME, Color.LIME, FontType.MONO, 35);
var startButton = new Button("Start Game");
var quitButton = new Button("Quit");

Slider volumeSlider = new Slider(0, 1, 1);
volumeSlider.setShowTickLabels(true);
volumeSlider.setShowTickMarks(true);
Expand All @@ -53,13 +55,14 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
volumeLabel.setText(String.format("Volume: %.0f%%", newValue.doubleValue() * 100));
}
});


try {


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

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

//adding image to manually mute music

Image mute = new Image(mutemusic_button);
ImageView imageView_mute = new ImageView(mute);
imageView_mute.setFitHeight(40);
imageView_mute.setFitWidth(50);
imageView_mute.setX(490);
imageView_mute.setY(20);
imageView_mute.setPreserveRatio(true);


Image audioOn = new Image(audioOnButton);
ImageView imageViewPlaying = new ImageView(audioOn);
imageViewPlaying.setFitHeight(40);
imageViewPlaying.setFitWidth(50);
imageViewPlaying.setX(490);
imageViewPlaying.setY(20);
imageViewPlaying.setPreserveRatio(true);


startButton.setMinSize(50, 50);
startButton.setPrefSize(140,60);

quitButton.setMinSize(140, 50);

title.setTranslateY(100);
Expand All @@ -93,7 +101,7 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
quitButton.setTranslateY(500);
quitButton.setTranslateX(getAppWidth() / 2 - 50);
quitButton.setStyle("-fx-font-size:20");

BorderPane root = new BorderPane();
root.setTop(title);
BorderPane.setAlignment(title, Pos.CENTER);
Expand All @@ -113,22 +121,31 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
mainMenuSound.stop();
});

imageView_mute.setOnMouseClicked(event -> {
if (mainMenuSound.getStatus() == MediaPlayer.Status.PLAYING) {
mainMenuSound.pause();
} else {
mainMenuSound.play();
}
imageViewPlaying.setOnMouseClicked(mouseEvent -> {
if (mainMenuSound.isMute()){
mainMenuSound.setMute(false);
imageViewPlaying.setImage(audioOn);
} else {
mainMenuSound.setMute(true);
imageViewPlaying.setImage(mute);
}
});

quitButton.setOnAction(event -> fireExit());

getContentRoot().getChildren().addAll(
bg, title, startButton, quitButton, imageView, imageView_mute,volumeSlider, volumeLabel
bg, title, startButton, quitButton, imageView, imageViewPlaying, volumeSlider, volumeLabel
);
}
catch (FileNotFoundException e){
System.out.println("File not found" + e.getMessage());
System.out.println("File not found" + e.getMessage());
}
}
@Override
public void onEnteredFrom(Scene prevState) {
super.onEnteredFrom(prevState);
FXGL.getAudioPlayer().stopAllSounds();
mainMenuSound.play();
}

}
}
Binary file added src/main/resources/assets/textures/playing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading