Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9152903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:10:06+00:00 2026-06-17T12:10:06+00:00

I have a canvas game which calls a function incScore every time an action

  • 0

I have a canvas game which calls a function incScore every time an action is performed in the game to increase the score.

Inside incScore I have a few if statements to draw a particular image to represent a level number on the canvas.

I also want to have a sound play once per level up. The way I’ve gone about things the lvlup sound will play every time the score matches the if statement.

Can anyone please help me get this so that the sound will only play once when the level changes and not again until the next level change? I’m also mention I’m using jQuery incase it has anything that could help me.

incScore(); //everytime an action in the game causes the score to increase

function incScore(){


    if (scoreTotal < 500){
        lvlimg = "L01";
        drawLevel(lvlimg);
        lvlupSound();
    }

    else if (scoreTotal > 500  && scoreTotal < 1000){
        lvlimg = "L02";
        drawLevel(lvlimg);
        lvlupSound();
    }

    else{
        lvlimg = "L03";
        drawLevel(lvlimg);
        lvlupSound();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:10:07+00:00Added an answer on June 17, 2026 at 12:10 pm

    You could shorten your function and use a semi static property to save the state. Using that, you can compare the current level to the previous and play a sound if they differ.

    function incScore(){
            incScore.level = incScore.level || 'L0'; //< initialize property
            lvlimg = "L0" + scoreTotal < 500 ? 1 : scoreTotal < 1000 ? 2 : 3; 
            drawLevel(lvlimg);
            if (incScore.level!=='L0' && 
                incScore.level !== lvlimg) { lvlupSound(); };
            //  ^compare local level to current
            incScore.level = lvlimg;
            //             ^ update local level
    }
    

    [edit, based on comment] The third line is a so called ternary, or conditional operator. See MDN. You can use more conditions.

    To avoid playing a sound before the score has reached a first level, you could use
    if (incScore.level!=='L0' && incScore.level !== lvlimg).

    I’ve created a mockup jsFiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Canvas which has many components inside it and those again, have
I have a canvas onto which I am drawing a JavaScript game. The problem
So i have been trying to make my canvas game work in real time
I have a <canvas> based game which I want to port to Air. The
I have an iPad 2 canvas app (game) and would like to get it
I am making a game engine using HTML5 canvas, and I have decided to
I have this canvas mousedown event: $('#canvas').mousedown(function(e) { var coords = canvas.getMousePos(e); // user
I have a canvas which contains a single myComponentA. A myComponentA contains a MyComponentB.
I have a game that I created using three.js , which is entirely rendered
I have a fullscreen program, which draws a lot of things for the game

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.