2023-09-05 23:50:10 -05:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
< html >
< head >
< title > BouncingBall.java< / title >
< meta http-equiv = "content-type" content = "text/html; charset=UTF-8" >
< style type = "text/css" >
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.number {color: #6897bb}
.string {color: #6a8759}
.ST1 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
-->
< / style >
< / head >
< body >
< table width = "100%" > < tr > < td align = "center" > /home/caleb/ASDV-Java/Semester 3/Assignments/MP2-chapter4_CalebFontenot/src/MP2_chapter4_CalebFontenot/BouncingBall.java< / td > < / tr > < / table >
< pre >
< span class = "comment" > /*< / span >
< span class = "comment" > * Click < / span > < span class = "ST0" > nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt< / span > < span class = "comment" > to change this license< / span >
< span class = "comment" > * Click < / span > < span class = "ST0" > nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java< / span > < span class = "comment" > to edit this template< / span >
< span class = "comment" > */< / span >
< span class = "literal" > package< / span > MP2_chapter4_CalebFontenot;
< span class = "literal" > import< / span > java.io.File;
2023-09-08 09:17:53 -05:00
< span class = "literal" > import< / span > java.util.ArrayList;
< span class = "literal" > import< / span > java.util.List;
2023-09-05 23:50:10 -05:00
< span class = "literal" > import< / span > javafx.animation.KeyFrame;
< span class = "literal" > import< / span > javafx.animation.Timeline;
< span class = "literal" > import< / span > javafx.beans.property.DoubleProperty;
< span class = "literal" > import< / span > javafx.event.ActionEvent;
< span class = "literal" > import< / span > javafx.event.EventHandler;
< span class = "literal" > import< / span > javafx.scene.control.Label;
< span class = "literal" > import< / span > javafx.scene.layout.GridPane;
< span class = "literal" > import< / span > javafx.scene.layout.Pane;
< span class = "literal" > import< / span > javafx.scene.media.Media;
< span class = "literal" > import< / span > javafx.scene.media.MediaPlayer;
< span class = "literal" > import< / span > javafx.scene.paint.Color;
< span class = "literal" > import< / span > javafx.scene.shape.Circle;
< span class = "literal" > import< / span > javafx.scene.shape.Rectangle;
< span class = "literal" > import< / span > javafx.util.Duration;
< span class = "comment" > /**< / span >
< span class = "comment" > *< / span >
< span class = "comment" > * < / span > < span class = "comment" > @author< / span > < span class = "comment" > caleb< / span >
< span class = "comment" > */< / span >
< span class = "literal" > public< / span > < span class = "literal" > class< / span > BouncingBall < span class = "literal" > extends< / span > Pane {
2023-09-08 09:17:53 -05:00
< span class = "literal" > private< / span > List< MediaPlayer> < span class = "ST1" > mediaPlayers< / span > = < span class = "literal" > new< / span > ArrayList< > ();
2023-09-05 23:50:10 -05:00
GridPane < span class = "ST1" > textPane< / span > = < span class = "literal" > new< / span > GridPane();
2023-09-08 09:17:53 -05:00
< span class = "literal" > private< / span > String < span class = "ST1" > currentlyPlaying< / span > ;
< span class = "literal" > private< / span > < span class = "literal" > final< / span > < span class = "literal" > int< / span > < span class = "ST1" > COLLISION_COOLDOWN< / span > = < span class = "number" > 20< / span > ;
2023-09-05 23:50:10 -05:00
< span class = "literal" > private< / span > < span class = "literal" > long< / span > < span class = "ST1" > timeSinceLastCollisionEvent< / span > = < span class = "number" > 0< / span > ;
< span class = "literal" > final< / span > < span class = "literal" > double< / span > < span class = "ST1" > RACKET_LENGTH< / span > = < span class = "number" > 100< / span > ;
< span class = "literal" > final< / span > < span class = "literal" > double< / span > < span class = "ST1" > radius< / span > = < span class = "number" > 20< / span > ;
< span class = "literal" > private< / span > < span class = "literal" > double< / span > < span class = "ST1" > x< / span > = < span class = "ST1" > radius< / span > , < span class = "ST1" > y< / span > = < span class = "ST1" > radius< / span > ;
< span class = "literal" > private< / span > < span class = "literal" > double< / span > < span class = "ST1" > dx< / span > = < span class = "number" > 1< / span > , < span class = "ST1" > dy< / span > = < span class = "number" > 1< / span > ;
< span class = "literal" > private< / span > Circle < span class = "ST1" > circle< / span > = < span class = "literal" > new< / span > Circle(< span class = "ST1" > x< / span > , < span class = "ST1" > y< / span > , < span class = "ST1" > r< / span > < span class = "ST1" > adius< / span > );
< span class = "literal" > private< / span > Rectangle < span class = "ST1" > racket< / span > = < span class = "literal" > new< / span > Rectangle(< span class = "ST1" > R< / span > < span class = "ST1" > ACKET_LENGTH< / span > , < span class = "number" > 2< / span > < span class = "number" > 0< / span > );
< span class = "literal" > private< / span > Label < span class = "ST1" > infoLabel< / span > = < span class = "literal" > new< / span > Label(< span class = "string" > " < / span > < span class = "string" > Your mouse cursor must be inside the bounds of the window to play!< / span > < span class = "string" > " < / span > );
< span class = "literal" > private< / span > Label < span class = "ST1" > racketTime< / span > = < span class = "literal" > new< / span > Label();
< span class = "literal" > private< / span > Label < span class = "ST1" > ballCords< / span > = < span class = "literal" > new< / span > Label();
< span class = "literal" > private< / span > Label < span class = "ST1" > playerScoreLabel< / span > = < span class = "literal" > new< / span > Label(< span class = "string" > " < / span > < span class = "string" > Player Score: 0< / span > < span class = "string" > " < / span > );
< span class = "literal" > private< / span > Label < span class = "ST1" > computerScoreLabel< / span > = < span class = "literal" > new< / span > Label(< span class = "string" > " < / span > < span class = "string" > Computer Score: 0< / span > < span class = "string" > " < / span > );
2023-09-08 09:17:53 -05:00
< span class = "literal" > private< / span > Label < span class = "ST1" > nowPlaying< / span > = < span class = "literal" > new< / span > Label();
2023-09-05 23:50:10 -05:00
< span class = "literal" > private< / span > Timeline < span class = "ST1" > animation< / span > ;
< span class = "literal" > private< / span > < span class = "literal" > int< / span > < span class = "ST1" > computerScore< / span > , < span class = "ST1" > playerScore< / span > = < span class = "number" > 0< / span > ;
< span class = "literal" > public< / span > BouncingBall() {
< span class = "ST1" > circle< / span > .setFill(Color.< span class = "ST2" > RED< / span > ); < span class = "comment" > // Set ball color< / span >
< span class = "ST1" > racket< / span > .setFill(Color.< span class = "ST2" > BLUE< / span > );
< span class = "ST1" > textPane< / span > .add(< span class = "ST1" > r< / span > < span class = "ST1" > acketTime< / span > , < span class = "number" > 0< / span > , < span class = "number" > 0< / span > );
< span class = "ST1" > textPane< / span > .add(< span class = "ST1" > b< / span > < span class = "ST1" > allCords< / span > , < span class = "number" > 0< / span > , < span class = "number" > 1< / span > );
< span class = "ST1" > textPane< / span > .add(< span class = "ST1" > p< / span > < span class = "ST1" > layerScoreLabel< / span > , < span class = "number" > 0< / span > , < span class = "number" > 2< / span > );
< span class = "ST1" > textPane< / span > .add(< span class = "ST1" > c< / span > < span class = "ST1" > omputerScoreLabel< / span > , < span class = "number" > 0< / span > , < span class = "number" > 3< / span > );
2023-09-08 09:17:53 -05:00
< span class = "ST1" > textPane< / span > .add(< span class = "ST1" > n< / span > < span class = "ST1" > owPlaying< / span > , < span class = "number" > 0< / span > , < span class = "number" > 4< / span > );
getChildren().addAll(< span class = "ST1" > c< / span > < span class = "ST1" > ircle< / span > , < span class = "ST1" > r< / span > < span class = "ST1" > acket< / span > , < span class = "ST1" > t< / span > < span class = "ST1" > extPane< / span > ); < span class = "comment" > // Place a ball into this pane< / span >
2023-09-05 23:50:10 -05:00
< span class = "ST1" > racket< / span > .relocate(< span class = "number" > 0< / span > , < span class = "number" > 5< / span > < span class = "number" > 80< / span > );
2023-09-08 09:17:53 -05:00
playSound(< span class = "string" > " < / span > < span class = "string" > bgm< / span > < span class = "string" > " < / span > );
2023-09-05 23:50:10 -05:00
< span class = "comment" > // Create an animation for moving the ball< / span >
< span class = "ST1" > animation< / span > = < span class = "literal" > new< / span > Timeline(< span class = "literal" > new< / span > KeyFrame(Duration.< span class = "ST3" > millis< / span > (< span class = "number" > 1< / span > ), < span class = "literal" > new< / span > EventHandler< ActionEvent> () {
@Override
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > handle< / span > (ActionEvent t) {
2023-09-08 09:17:53 -05:00
2023-09-05 23:50:10 -05:00
< span class = "ST1" > timeSinceLastCollisionEvent< / span > ++;
< span class = "ST1" > racketTime< / span > .setText(< span class = "string" > " < / span > < span class = "string" > Frames since last collision: < / span > < span class = "string" > " < / span > + < span class = "ST1" > timeSinceLastCollisionEvent< / span > );
moveBall();
2023-09-08 09:17:53 -05:00
< span class = "comment" > // Process collisions< / span >
< span class = "literal" > if< / span > (< span class = "ST1" > y< / span > > = (getHeight() - < span class = "number" > 20< / span > ) & & (< span class = "ST1" > timeSinceLastCollisionEvent< / span > > < span class = "ST1" > COLLISION_COOLDOWN< / span > )) {
2023-09-05 23:50:10 -05:00
< span class = "ST1" > timeSinceLastCollisionEvent< / span > = < span class = "number" > 0< / span > ;
2023-09-08 09:17:53 -05:00
playSound(< span class = "string" > " < / span > < span class = "string" > collision< / span > < span class = "string" > " < / span > );
2023-09-05 23:50:10 -05:00
incrementComputerScore();
}
2023-09-08 09:17:53 -05:00
< span class = "literal" > if< / span > ((< span class = "ST1" > x< / span > < = < span class = "number" > 20< / span > & & (< span class = "ST1" > timeSinceLastCollisionEvent< / span > > < span class = "ST1" > COLLISION_COOLDOWN< / span > )) || ((< span class = "ST1" > x< / span > > = getWidth() - < span class = "number" > 20< / span > ) & & (< span class = "ST1" > timeSinceLastCollisionEvent< / span > > < span class = "ST1" > COLLISION_COOLDOWN< / span > ))) {
< span class = "ST1" > timeSinceLastCollisionEvent< / span > = < span class = "number" > 0< / span > ;
playSound(< span class = "string" > " < / span > < span class = "string" > collision< / span > < span class = "string" > " < / span > );
}
< span class = "literal" > if< / span > (< span class = "ST1" > y< / span > < = < span class = "number" > 20< / span > & & (< span class = "ST1" > timeSinceLastCollisionEvent< / span > > < span class = "ST1" > COLLISION_COOLDOWN< / span > )) {
< span class = "ST1" > timeSinceLastCollisionEvent< / span > = < span class = "number" > 0< / span > ;
playSound(< span class = "string" > " < / span > < span class = "string" > collision< / span > < span class = "string" > " < / span > );
}
2023-09-05 23:50:10 -05:00
}
})
);
< span class = "ST1" > animation< / span > .setRate(< span class = "ST1" > animation< / span > .getRate() * < span class = "number" > .5< / span > );
< span class = "ST1" > animation< / span > .setCycleCount(Timeline.< span class = "ST2" > INDEFINITE< / span > );
}
< span class = "literal" > private< / span > < span class = "literal" > boolean< / span > < span class = "ST4" > processRacketCollision< / span > () {
< span class = "literal" > boolean< / span > racketCollision = < span class = "ST1" > racket< / span > .getBoundsInParent().intersects(< span class = "ST1" > c< / span > < span class = "ST1" > ircle< / span > .getBoundsInParent());
2023-09-08 09:17:53 -05:00
< span class = "literal" > if< / span > (racketCollision & & (< span class = "ST1" > timeSinceLastCollisionEvent< / span > > < span class = "ST1" > COLLISION_COOLDOWN< / span > )) { < span class = "comment" > // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.< / span >
2023-09-05 23:50:10 -05:00
System.< span class = "ST2" > out< / span > .println(< span class = "string" > " < / span > < span class = "string" > Racket collision detected!< / span > < span class = "string" > " < / span > );
< span class = "ST1" > timeSinceLastCollisionEvent< / span > = < span class = "number" > 0< / span > ;
incrementPlayerScore();
2023-09-08 09:17:53 -05:00
playSound(< span class = "string" > " < / span > < span class = "string" > racket< / span > < span class = "string" > " < / span > );
2023-09-05 23:50:10 -05:00
< span class = "literal" > return< / span > < span class = "literal" > true< / span > ;
} < span class = "literal" > else< / span > {
< span class = "literal" > return< / span > < span class = "literal" > false< / span > ;
}
}
< span class = "literal" > private< / span > < span class = "literal" > void< / span > < span class = "ST4" > moveBall< / span > () {
< span class = "comment" > // Check boundaries< / span >
< span class = "literal" > if< / span > (< span class = "ST1" > x< / span > < < span class = "ST1" > radius< / span > || < span class = "ST1" > x< / span > > getWidth() - < span class = "ST1" > radius< / span > ) {
< span class = "ST1" > dx< / span > *= -< span class = "number" > 1< / span > ; < span class = "comment" > // Change ball move direction< / span >
}
< span class = "literal" > if< / span > (< span class = "ST1" > y< / span > < < span class = "ST1" > radius< / span > || < span class = "ST1" > y< / span > > getHeight() - < span class = "ST1" > radius< / span > || processRacketCollision()) {
< span class = "ST1" > dy< / span > *= -< span class = "number" > 1< / span > ; < span class = "comment" > // Change ball move direction< / span >
}
< span class = "comment" > // Adjust ball position by 1 or -1< / span >
< span class = "ST1" > x< / span > += < span class = "ST1" > dx< / span > ;
< span class = "ST1" > y< / span > += < span class = "ST1" > dy< / span > ;
< span class = "ST1" > circle< / span > .setCenterX(< span class = "ST1" > x< / span > );
< span class = "ST1" > circle< / span > .setCenterY(< span class = "ST1" > y< / span > );
ballCoordsToLabel();
}
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > play< / span > () {
< span class = "ST1" > animation< / span > .play();
}
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > pause< / span > () {
< span class = "ST1" > animation< / span > .pause();
}
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > increaseSpeed< / span > () {
< span class = "ST1" > animation< / span > .setRate(< span class = "ST1" > animation< / span > .getRate() * < span class = "number" > 1.5< / span > );
System.< span class = "ST2" > out< / span > .println(< span class = "ST1" > a< / span > < span class = "ST1" > nimation< / span > .getRate());
}
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > decreaseSpeed< / span > () {
< span class = "ST1" > animation< / span > .setRate(< span class = "ST1" > animation< / span > .getRate() * < span class = "number" > 1.5< / span > > < span class = "number" > 0< / span > ? < span class = "ST1" > animation< / span > .getRate() / < span class = "number" > 1.5< / span > : < span class = "number" > 0< / span > );
System.< span class = "ST2" > out< / span > .println(< span class = "ST1" > a< / span > < span class = "ST1" > nimation< / span > .getRate());
}
< span class = "literal" > public< / span > DoubleProperty < span class = "ST4" > rateProperty< / span > () {
< span class = "literal" > return< / span > < span class = "ST1" > animation< / span > .rateProperty();
}
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > moveRacket< / span > (< span class = "literal" > double< / span > x) {
< span class = "ST1" > racket< / span > .relocate(x, < span class = "number" > 5< / span > < span class = "number" > 80< / span > );
}
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > showInfoLabel< / span > () {
< span class = "literal" > double< / span > paneHeight = getHeight();
< span class = "literal" > double< / span > paneWidth = getWidth();
getChildren().add(< span class = "ST1" > i< / span > < span class = "ST1" > nfoLabel< / span > );
< span class = "comment" > // Center Text< / span >
< span class = "ST1" > infoLabel< / span > .relocate(paneWidth / < span class = "number" > 4.0< / span > , paneHeight / < span class = "number" > 2.0< / span > );
}
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > hideInfoLabel< / span > () {
getChildren().remove(< span class = "ST1" > i< / span > < span class = "ST1" > nfoLabel< / span > );
}
< span class = "literal" > private< / span > < span class = "literal" > void< / span > < span class = "ST4" > incrementPlayerScore< / span > () {
< span class = "ST1" > playerScoreLabel< / span > .setText(< span class = "string" > " < / span > < span class = "string" > Player score: < / span > < span class = "string" > " < / span > + ++< span class = "ST1" > playerScore< / span > );
}
< span class = "literal" > private< / span > < span class = "literal" > void< / span > < span class = "ST4" > incrementComputerScore< / span > () {
< span class = "ST1" > computerScoreLabel< / span > .setText(< span class = "string" > " < / span > < span class = "string" > Computer score: < / span > < span class = "string" > " < / span > + ++< span class = "ST1" > computerScore< / span > );
2023-09-08 09:17:53 -05:00
2023-09-05 23:50:10 -05:00
}
< span class = "literal" > private< / span > < span class = "literal" > void< / span > < span class = "ST4" > ballCoordsToLabel< / span > () {
< span class = "ST1" > ballCords< / span > .setText(< span class = "string" > " < / span > < span class = "string" > Ball coords: < / span > < span class = "string" > " < / span > + < span class = "ST1" > x< / span > + < span class = "string" > " < / span > < span class = "string" > , < / span > < span class = "string" > " < / span > + < span class = "ST1" > y< / span > );
}
2023-09-08 09:17:53 -05:00
< span class = "literal" > private< / span > < span class = "literal" > void< / span > < span class = "ST4" > playSound< / span > (String sampleType) {
< span class = "literal" > boolean< / span > isSfx = < span class = "literal" > true< / span > ;
String sample = < span class = "string" > " < / span > < span class = "string" > sound/sfx/Sample_00< / span > < span class = "string" > " < / span > ;
2023-09-05 23:50:10 -05:00
< span class = "literal" > int< / span > randInt = (< span class = "literal" > int< / span > ) (Math.< span class = "ST3" > random< / span > () * < span class = "number" > 2< / span > );
2023-09-08 09:17:53 -05:00
< span class = "literal" > switch< / span > (sampleType) {
< span class = "literal" > case< / span > < span class = "string" > " < / span > < span class = "string" > collision< / span > < span class = "string" > " < / span > :
randInt = (< span class = "literal" > int< / span > ) (Math.< span class = "ST3" > random< / span > () * < span class = "number" > 2< / span > );
isSfx = < span class = "literal" > true< / span > ;
< span class = "literal" > switch< / span > (randInt) {
< span class = "literal" > case< / span > < span class = "number" > 0< / span > :
sample += < span class = "string" > " < / span > < span class = "string" > 05< / span > < span class = "string" > " < / span > ;
< span class = "literal" > break< / span > ;
< span class = "literal" > case< / span > < span class = "number" > 1< / span > :
sample += < span class = "string" > " < / span > < span class = "string" > 07< / span > < span class = "string" > " < / span > ;
< span class = "literal" > break< / span > ;
}
< span class = "literal" > break< / span > ;
< span class = "literal" > case< / span > < span class = "string" > " < / span > < span class = "string" > racket< / span > < span class = "string" > " < / span > :
randInt = (< span class = "literal" > int< / span > ) (Math.< span class = "ST3" > random< / span > () * < span class = "number" > 2< / span > );
isSfx = < span class = "literal" > true< / span > ;
< span class = "literal" > switch< / span > (randInt) {
< span class = "literal" > case< / span > < span class = "number" > 0< / span > :
sample += < span class = "string" > " < / span > < span class = "string" > 10< / span > < span class = "string" > " < / span > ;
< span class = "literal" > break< / span > ;
< span class = "literal" > case< / span > < span class = "number" > 1< / span > :
sample += < span class = "string" > " < / span > < span class = "string" > 12< / span > < span class = "string" > " < / span > ;
< span class = "literal" > break< / span > ;
}
< span class = "literal" > break< / span > ;
< span class = "literal" > case< / span > < span class = "string" > " < / span > < span class = "string" > bgm< / span > < span class = "string" > " < / span > :
sample = < span class = "string" > " < / span > < span class = "string" > sound/bgm/< / span > < span class = "string" > " < / span > ;
randInt = (< span class = "literal" > int< / span > ) (Math.< span class = "ST3" > random< / span > () * < span class = "number" > 3< / span > );
isSfx = < span class = "literal" > false< / span > ;
< span class = "literal" > switch< / span > (randInt) {
< span class = "literal" > case< / span > < span class = "number" > 0< / span > :
< span class = "ST1" > currentlyPlaying< / span > = < span class = "string" > " < / span > < span class = "string" > Caleb Fontenot - Blips< / span > < span class = "string" > " < / span > ;
< span class = "ST1" > nowPlaying< / span > .setText(< span class = "string" > " < / span > < span class = "string" > Now playing: < / span > < span class = "string" > " < / span > + < span class = "ST1" > currentlyPlaying< / span > );
< span class = "literal" > break< / span > ;
< span class = "literal" > case< / span > < span class = "number" > 1< / span > :
< span class = "ST1" > currentlyPlaying< / span > = < span class = "string" > " < / span > < span class = "string" > Caleb Fontenot - DEAL WITH IT< / span > < span class = "string" > " < / span > ;
< span class = "ST1" > nowPlaying< / span > .setText(< span class = "string" > " < / span > < span class = "string" > Now playing: < / span > < span class = "string" > " < / span > + < span class = "ST1" > currentlyPlaying< / span > );
< span class = "literal" > break< / span > ;
< span class = "literal" > case< / span > < span class = "number" > 2< / span > :
< span class = "ST1" > currentlyPlaying< / span > = < span class = "string" > " < / span > < span class = "string" > Caleb Fontenot - Tomodachi Moment< / span > < span class = "string" > " < / span > ;
< span class = "ST1" > nowPlaying< / span > .setText(< span class = "string" > " < / span > < span class = "string" > Now playing: < / span > < span class = "string" > " < / span > + < span class = "ST1" > currentlyPlaying< / span > );
< span class = "literal" > break< / span > ;
}
sample += < span class = "ST1" > currentlyPlaying< / span > ;
< span class = "literal" > break< / span > ;
2023-09-05 23:50:10 -05:00
}
2023-09-08 09:17:53 -05:00
Media sound = < span class = "literal" > new< / span > Media(< span class = "literal" > n< / span > < span class = "literal" > ew< / span > File(sample + < span class = "string" > " < / span > < span class = "string" > .wav< / span > < span class = "string" > " < / span > ).toURI().toString());
2023-09-05 23:50:10 -05:00
MediaPlayer mediaPlayer = < span class = "literal" > new< / span > MediaPlayer(sound);
mediaPlayer.setStartTime(Duration.< span class = "ST2" > ZERO< / span > );
2023-09-08 09:17:53 -05:00
mediaPlayer.setAutoPlay(< span class = "literal" > t< / span > < span class = "literal" > rue< / span > );
< span class = "comment" > // If we' re playing BGM, lower the volume a little< / span >
< span class = "literal" > if< / span > (!isSfx) {
mediaPlayer.setVolume(< span class = "number" > 0< / span > < span class = "number" > .6< / span > );
mediaPlayer.setCycleCount(MediaPlayer.< span class = "ST2" > INDEFINITE< / span > );
}
2023-09-05 23:50:10 -05:00
mediaPlayer.play();
2023-09-08 09:17:53 -05:00
< span class = "ST1" > mediaPlayers< / span > .add(mediaPlayer); < span class = "comment" > // Prevent the JVM' s GC from deleting our media player if there' s already one running< / span >
< span class = "comment" > // Free the media player when it has finished playing a sound.< / span >
mediaPlayer.setOnEndOfMedia(< span class = "literal" > new< / span > Runnable() {
@Override
< span class = "literal" > public< / span > < span class = "literal" > void< / span > < span class = "ST4" > run< / span > () {
< span class = "comment" > //System.out.println(" I' m done playing sound! Please take me to the ether, Java GC!" );< / span >
mediaPlayer.stop();
mediaPlayer.dispose();
< span class = "ST1" > mediaPlayers< / span > .remove(mediaPlayer);
}
});
2023-09-05 23:50:10 -05:00
}
}
< / pre > < / body >
< / html >