Skip to content

Commit

Permalink
Merge pull request #108 from Thompson6626/Fixingmusicandsoundbutton
Browse files Browse the repository at this point in the history
Fixed the music button on the main menu not working
  • Loading branch information
jvondermarck authored Aug 26, 2024
2 parents 57c7e8a + 14e54ed commit c037483
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
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.

0 comments on commit c037483

Please sign in to comment.