Designing a final splash screen for my game with the help of JavaScript, Cascading Style Sheets, and

I'm facing a challenge with setting up an end screen for my candy crush game. I have implemented a timer, but when the time runs out and certain conditions are met, the end screen fails to appear. Do you know what might be causing this issue?

function timer() {
    var seconds = 120;
    var timer = setInterval(function() {
        document.getElementById("safeTimerDisplay").innerHTML = seconds;
        seconds--;
        if (seconds < 0) {
            clearInterval(timer);
        }
    }, 1000);
}

timer();


function endGame() {
    if (seconds === 0 && score > 1800) {
        console.log(endWindow1)
    }
}
#EndWindow1 {
    background: url("./endscreen.jpg") no-repeat center center fixed;
    background-size: cover;
    font-family: Arial, Helvetica, sans-serif;
    color: white;
    text-align: center;
}
<div id="EndWindow1" style="display:none;"></div>
<h1>Timer: <span id="safeTimerDisplay"></span></h1>

In an attempt to showcase a specific image when the user's score surpasses 1800 and the timer hits zero, I have set up a conditional statement. However, upon reaching zero on the clock, nothing changes and the game continues as normal. It seems like something is interrupting the display of the end screen.

Answer №1

There are a number of concerns to address in this scenario:

  1. The element EndWindow1 lacks content and has a default height of auto, making it effectively invisible due to its empty nature.
  2. The function endGame() is never invoked within the code snippet.
  3. console.log() serves as a debugging tool for outputting information to the console, rather than toggling element visibility.
  4. A variable/constant that was not declared is being referenced instead of the intended element EndWindow1.

window.addEventListener('DOMContentLoaded', function() {
  const TIMER = document.getElementById('safeTimerDisplay');
  const END_WINDOW = document.getElementById('EndWindow1');
  let remaining_time = 10;
  let score = 1801;
  
  function timer() {
    let timer = setInterval(function() {
      TIMER.textContent = remaining_time;
      remaining_time--;
      switch (remaining_time) {
        case 0:
          endGame();
          break;
        case -1:
          clearInterval(timer);
          break;
        default:
          break;
      }
    }, 1000);
  }

  timer();

  function endGame() {
    END_WINDOW.classList.toggle('d-none', !(score > 1800));
  }
})
#EndWindow1 {
  background: url("https://via.placeholder.com/200.jpg") no-repeat center center fixed;
  background-size: cover;
  font-family: Arial, Helvetica, sans-serif;
  color: white;
  text-align: center;
  height: 200px;
}

.d-none {
  display: none;
}
<div id="EndWindow1" class="d-none"></div>
<h1>Timer: <span id="safeTimerDisplay"></span></h1>

Code Enhancement:

Rather than utilizing:

if (seconds === 0 && score > 1800) {
  console.log(endWindow1)
}

and evaluating two conditions separately, consider incorporating the call to endGame directly within the setInterval callback where timing is continuously monitored.

Eliminate the need for an if statement by managing element visibility through CSS class manipulation using classList.toggle to show or hide based on specified conditions.

Answer №2

function countdown() {
  var time = 120;
  var timerInterval = setInterval(function() {
    document.getElementById("countdownDisplay").innerHTML = time;
    time--;
    if (time < 0) {
      clearInterval(timerInterval);
    }
  }, 1000);
}

countdown();


function evaluateScore() {
  if (time === 0 && score > 1800) {
    console.log(gameOverMessage)
  }
}
#endScreen1 {
  background: url("./endscreen.jpg") no-repeat center center fixed;
  background-size: cover;
  font-family: Arial, Helvetica, sans-serif;
  color: white;
  text-align: center;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the PayPal Checkout JavaScript SDK's CreateOrder call and interpreting the response

I am currently exploring how to use the JavaScript SDK to display PayPal payment buttons on a website. I am new to working with JSON and REST API calls, so I am facing some challenges in implementing this. The createOrder function is running smoothly and ...

Update the blue glow effect on checkbox inputs within Bootstrap 4 to create a unique visual experience

This query has been asked repeatedly, and I have exhaustively attempted numerous solutions. Here is my code snippet: <div class="btn-group" id="btn-container" data-toggle="buttons"> <label class="btn classic-design"> <input type ...

Include the window.innerWidth value in the initial GET request

When dealing with a server-side rendered app that has responsive styling based on window.innerWidth, the challenge lies in how to pass the client's screen size in the initial GET request for index.html. This is crucial so that the server can prepare a ...

What is the best way to modify .js source files while using AjaxControlToolkit?

Currently, I am tackling the source code for AjaxControlToolkit in the VS2008 version. There are specific modifications I need to implement in the core javascript files like MicrosoftAjaxWebForms.js, but unfortunately, I am unable to locate the original so ...

Retrieve data from server using Angular and present content on a dedicated webpage

I am currently working on a page that displays all usernames, and when I click on a username, I want to retrieve more information from the server and display it on a separate User page (including first name, last name, etc). However, I am encountering an ...

Explore records within a specific date range AS WELL AS insert data within a defined date range

I'm working on developing a search application that allows users to view data based on the inputted date range. However, I also want to incorporate predefined date ranges into the final output. Although I've been searching for a solution, I have ...

Tips for aligning content to the right edge

Within a container class, I have a background image set up and divided it into col-6. The first col-6 is currently empty. However, my goal is to add content on the right side of this column. I attempted to achieve this by adding the justify-content-end c ...

Discover the method for invoking a Javascript function within a Leaflet popup using HTML

Upon clicking on a marker on the leaflet map, I aim to trigger a popup box that contains five elements: Title Description Image Button (Next Image) Button (Previous Image) To achieve this, I attempted to include a custom popup for each feature ...

The specified CSS background image is not correctly aligned with the dimensions of the selector

I have multiple non-local images that I want to use as background images for classes with a width of 500px and height of 330px. Even though these dimensions are specified in the CSS properties for the class, the background: url('http://www.example.com ...

Importing multiple modules in Typescript is a common practice

I need to include the 'express' module in my app. According to Mozilla's documentation, we should use the following code: import { Application }, * as Express from 'express' However, when using it in TypeScript and VSCode, I enc ...

How can I fully personalize the ion-toggle in Ionic?

When customizing <ion-toggle> within Ionic, I encounter an issue with toggle selection in CSS. Even though I have successfully customized two <ion-toggle> elements using CSS, I struggle to give them different customizations separately. In addi ...

Unable to toggle sidebar in Angular Material component

I'm trying to create a template similar to Youtube using angular material, but I've hit a roadblock with the sidebar navigation. Here are the requirements for the template: 1. The sidebar should be open by default (based on screen size) 2. When t ...

The ng-repeat ng-switch combination is not functioning as expected

My data can be simplified to the following in my code: function DemoCtrl($scope) { $scope.myData= [ {text: "blah", other: 3, V: 'caseOne'}, {text: "blah", other: 3, V: 'caseTwo'}, {text: "blah", other: 3, V ...

AngularJS $q - pausing execution to synchronize all promises

I've encountered a challenge that I haven't been able to solve through online searches. In my project, I am using a library for selecting items and performing custom modifications through callback functions. However, I need to execute some async ...

how to change visibility with Javascript

As I work on creating a menu using HTML and JavaScript, I've designed it to be a grid layout with 3 rows and 2 columns. The menu contents are also structured in a grid format. When the screen width is reduced, the layout changes to a single row and co ...

Issues with displaying images in IE 11 when using Flex

After testing this code, I found that it works perfectly in both Firefox and Chrome, displaying a row as expected. https://i.sstatic.net/29wgb.png However, when viewing the same row in IE 11, the image is not visible, and the width seems to be missing in ...

What's causing me to encounter two canvases while implementing PIXI.js in my Next.js project?

I'm facing a challenge in my game development project that utilizes PIXI.js within a Next.js setup. Currently, I am experiencing an issue where the webpage is displaying two canvases instead of one, leading to unexpected rendering issues and impacting ...

Designing versatile buttons with HTML

When accessing the website on a phone, I have trouble seeing and tapping on these two buttons because they are too far apart. I would like to change it so that once a file is selected, the 'choose file' button will be replaced by the upload butto ...

Add the item to a fresh array using the Ajax function

Here is an array that I have: var arrayOfResults = []; // Results after like statement After making a database call, I receive a JSON result like this: [{ "id": "{fcb42c9c-3617-4048-b2a0-2600775a4c34}", "pid": "{34214CCB-90C3-4D ...

Adding numerous objects to a Vuex store using mutations

I am currently working with the following store setup: import Vue from 'vue' import Vuex from 'vuex' import axios from 'axios' Vue.use(Vuex) export default new Vuex.Store({ plugins: [createPersistedState()], state: { ...