0

I have built a game using React (and Matter JS and p5.js). It mainly has 2 variables, score (increments everytime goal is hit) and timer (decrements every second).
I want music to play in the background if user wants. So I have an audio toggle button. But I have to use a state variable to ensure the audio is playing/pausing as required. Such a state update re-renders the component and thus resets the score and timer variables.

Is there any way to toggle the audio in React without using a state variable, avoiding re-rendering the component, or at least some way for React to know that if it gets re-rendered while in the "/game" page, it shouldn't reset the score and timer?

Here is the part of the code that handles the audio:

            audioButton = p.createImg(audioRef.current.paused ? audioOff : audioOn, "audio button");
            audioButton.position(20, p.height - 50);
            audioButton.size(30, 30);
            audioButton.mouseClicked(() => {
                if (audioRef.current.paused) {
                    audioRef.current.play();
                } else {
                    audioRef.current.pause();
                }
                setPlaying(prevPlaying => !prevPlaying);
                setTimeout(() => {
                    audioButton.elt.src = audioRef.current.paused ? audioOff : audioOn;
                }, 0);
            });

My score and timer are defined as follows:

//GLOBAL VARIABLES
let score = 0;
let timer = 0;

function Game() {  
    score = 0;
    timer = 0;


    /*REST OF THE GAME LOGIC...*/
}

I have already tried making score and timer state variables instead of global variables. There must be some p5.js issue in that case which doesn't update the score and the timer at ALL (they remain as 0 and 42 respectively).

Any help would be appreciated, thanks!

4
  • Hey. You should definitively use useRef instead of useState. refs do not trigger rerendering. Here is a quick pseudo code I put together. It might help gist.github.com/tonkec/0bf19b100fa446a656afdaa78e6417bc Commented Aug 27 at 12:10
  • Why do you need a state variable for playing in the first place? You can just use audioRef.current.paused for that too.
    – AKX
    Commented Aug 27 at 12:20
  • This is missing a minimal reproducible example. Please share runnable code with enough context to make the problem reproducible. Thanks!
    – ggorlen
    Commented Aug 27 at 21:23
  • 1
    @AntonijaŠimić thanks a lot! Turns out I was using useRef anyway but for some reason I had kept my previous attempt at using state variables, so it was just re-rendering the page for no reason. Clean code really is a necessity!
    – Worker1432
    Commented Aug 29 at 19:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.