Skip to content

Commit be7f61b

Browse files
feat: Improve game sound logic
1 parent e1ff33d commit be7f61b

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
55

6+
## [2.2](../../releases/tag/v2.2)
7+
- Improve game sound logic
8+
69
## [2.1](../../releases/tag/v2.1)
710
- Improve Game Over screen with sounds and animations
811
- Add scaling strategy for components styles

src/components/screens/GameOver.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class GameOver extends React.Component {
6666
await Audio.setIsEnabledAsync(true);
6767
const soundObject = new Audio.Sound();
6868
try {
69+
await soundObject.unloadAsync();
6970
await soundObject.loadAsync((scorePercent >= 0.8) ? GOOD_SOUND: (scorePercent > 0.5) ? AVERAGE_SOUND: BAD_SOUND);
7071
await soundObject.playAsync();
7172
} catch (error) {

src/components/screens/TriviaGame.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import Question from '../Question';
1313
import TriviaLoader from '../TriviaLoader';
1414
import * as actions from '../../actions';
1515
import { capitalizeFirstLetter } from '../../Utils';
16-
import { scale, moderateScale, verticalScale} from '../../Scaling';
16+
import { scale, moderateScale } from '../../Scaling';
1717

18-
const CORRECT_SOUND = require('../../../assets/sounds/correct.wav');
19-
const INCORRECT_SOUND = require('../../../assets/sounds/incorrect.wav');
20-
const TIMEOUT_SOUND = require('../../../assets/sounds/timeout.wav');
18+
// Sound setup
19+
const AVAILABLE_SOUNDS = {
20+
correct: require('../../../assets/sounds/correct.wav'),
21+
incorrect: require('../../../assets/sounds/incorrect.wav'),
22+
timeout: require('../../../assets/sounds/timeout.wav')
23+
};
2124

2225
const COUNTDOWN_TIME = 10;
2326

@@ -40,6 +43,7 @@ class TriviaGame extends React.Component {
4043
answerType: 'correct',
4144
fontLoaded: false,
4245
countdownTime: COUNTDOWN_TIME,
46+
soundController: null,
4347
};
4448
}
4549

@@ -56,15 +60,22 @@ class TriviaGame extends React.Component {
5660
);
5761
}
5862

63+
async componentDidMount() {
64+
// Preload sound controller
65+
await Audio.setIsEnabledAsync(true);
66+
this.setState({
67+
soundController: new Audio.Sound()
68+
});
69+
}
70+
5971
/**
60-
* Play the correct sound based on answer status type
72+
* Play the correct sound based on answer status type.
6173
*/
6274
playSound = async (type) => {
63-
await Audio.setIsEnabledAsync(true);
64-
const soundObject = new Audio.Sound();
6575
try {
66-
await soundObject.loadAsync((type === 'correct') ? CORRECT_SOUND: (type === 'incorrect') ? INCORRECT_SOUND: TIMEOUT_SOUND);
67-
await soundObject.playAsync();
76+
await this.state.soundController.unloadAsync();
77+
await this.state.soundController.loadAsync(AVAILABLE_SOUNDS[type]);
78+
await this.state.soundController.playAsync();
6879
} catch (error) {
6980
// An error occurred!
7081
console.log(error);

0 commit comments

Comments
 (0)