Image of sprite is not displayed

Having trouble displaying a sprite.

Here is the HTML file :

<head>
    <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.7.5/phaser.min.js"></script>
</head>
<body>
    <div id="game"></div>
    <script src="main.js"></script>
</body>

Contents of main.js :

(() => {
    var preload = () => {
        Game.load.image("player", "assets/player.png");
    },

    create = () => {
        Game.add.sprite(225, 450, "player");
    },

    update = () => {

    },

    Game = new Phaser.Game(500, 500, Phaser.AUTO, "game",
        {
            preload : "preload",
            create : "create",
            update : "update"
        }
    );
})();

The sprite is not displaying and there is only a black background. The Firefox developer console shows an error:

TypeError: this.onPreloadCallback.call is not a function
.

Answer №1

When defining the var Game, it is crucial to correctly reference the functions defined earlier.

(() => {
    var preload = () => {
        Game.load.image("player", "assets/player.png");
    },

    create = () => {
        Game.add.sprite(225, 450, "player");
    },

    update = () => {

    };

    var Game = new Phaser.Game(500, 500, Phaser.AUTO, "game",
    {
        preload : preload,
        create : create,
        update : update
    });
})();

This code block is the same as:

(() => {
    var Game = new Phaser.Game(500, 500, Phaser.AUTO, "game",
    {
        preload : () => {
            Game.load.image("player", "assets/player.png");
        },
        create : () => {
            Game.add.sprite(225, 450, "player");
        },
        update : () => {}
    });
})();

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

Node.js Axios Returns Bad Request with Status Code 400

I am currently facing an issue while trying to send the results of a nodejs query to an endpoint. Interestingly, I receive a successful response when using Postman with the correct parameters, but encounter errors when attempting to use axios. data: ' ...

Tips for ensuring elements within a modal receive immediate focus when opened in Angular 2

I am relatively new to Angular JS and I am encountering some challenges with implementing a directive in Angular 2 that can manage focusing on the modal when it is opened by clicking a button. There have been similar queries in the past, with solutions pr ...

Updating Mysql through REST API/JWT using PUT method is not possible

I have been attempting to send an update request using Jwt (Tokens) and Node.Js with a backend in mysql. While Postman confirms that the record has been successfully updated, I am unable to locate where the actual update occurred. No changes seem to be ref ...

Automatically navigate to a specific selection within the Autocomplete feature

Imagine having a dropdown or Autocomplete list in Material-UI with a long list of items. How can you make the list automatically scroll to a specific item when you open the dropdown? For instance, if we take the list of top100Films, the initial displayed i ...

Issue encountered while attempting to save the date to a json file

While working on my code to directly print the date from index.js into data.json, I encountered an error stating that data is not defined. This is what I have done so far: Ran npm init Installed jsonfile using npm i jsonfile index.js const json ...

Center a span vertically inside a div as the text within the span grows to occupy the entire div

I am facing an issue with a table that has 25 td's. Each td contains specific elements as shown below: <td> <div> <span> text </span> </div> </td> Additionally, there is a script in place that adj ...

Leveraging useContext to alter the state of a React component

import { createContext, useState } from "react"; import React from "react"; import axios from "axios"; import { useContext } from "react"; import { useState } from "react"; import PermIdentityOutlinedIcon f ...

Using JavaScript regex to split text by line breaks

What is the best way to split a long string of text into individual lines? And why does this code snippet return "line1" twice? /^(.*?)$/mg.exec('line1\r\nline2\r\n'); ["line1", "line1"] By enabling the multi-line modifi ...

React components do not re-render when the context value changes

The issue with React not re-rendering when the context value changes persists I am using tailwindcss, React(v18.2.0), and vite(3.2.4). I have already attempted i want that clicking on TodoItem should change the completed value in the todo using React con ...

Button fails to display as intended despite meeting conditions

I'm currently using a formData object with useState(). Whenever a field is updated in the form, I update formData like this: setFormData({...formData, [field.id]: field.value}) My goal is to have the button at the end of the form change once all req ...

Instructions on dynamically positioning a collection of div elements at the center of a webpage container

I am looking to create a set of divs that are centered on the page and adjust in size according to the length of the username. .Container1 { display: table; width: 100%; padding:0; margin:0; -webkit-box-sizing: border-box; -moz-box-sizing: bor ...

After updating to the latest npm version, the NodeJS server continues to display the error message "Upgrade Required" when loading pages

After developing a Node project using NodeJS version 5.4.x and NPM version 3.3.12 on Windows, I encountered an issue where the project throws an "Upgrade Required" message (HTTP Error code - 426) upon loading the page after some time of inactivity. To add ...

Customize the appearance of every other column in an asp gridview

Looking for help with formatting rows and columns in an ASP GridView. The rows are automatically colored alternating, and I want to make the content in every first column bold and every second column normal. I have heard about using CSS nth-child() to achi ...

The use of the || operator within arguments

I am facing a challenge: //this console log displays correct value console.log('localstorage', localStorage.getItem('subMenu')); setSubMenu( JSON.parse(localStorage.getItem('subMenu') || JSON.stringify(s ...

Sequence of promises: The parent promise does not wait for the child promise to be executed before moving on

I am retrieving data from a particular mongoDB collection. Within that response, I obtain the ID associated with another collection's data and merge it into one cohesive object. Take a look at my code snippet below. It seems like it's not waitin ...

Having trouble retrieving a JSON Element in JavaScript using the Object's property

I am having trouble retrieving a specific JSON element from the following JSON structure: { "ACTION": "AA", "MESSAGE": "Customer: 30xxx Already Approved on 2017/01/01" } Even though I receive the data in JSON format, attempting to access data.ACTION or d ...

Issue with Angular Material Table: Dragged rows do not drop in the correct position when scrolling

I'm encountering issues with using Angular Material Table along with Drag and Drop CDK and scrolling. While dragging a row and then scrolling, the row does not drop where intended. Additionally, the animation does not follow the scroll correctly. I ...

What are the options for app directory routing and programmatic navigation in the upcoming 13 application

I am currently working on a project called Next 13 that involves using the app directory and MUI 5. The project's structure is organized as follows: ./src ./src/app ./src/app/dc ./src/app/dc/admin ./src/app/dc/admin/dc_types.jsx However, when I try t ...

What is the best way to retrieve a file's creation date using the file System module in Node.js

I'm having trouble fetching the filename and file creation date to send to a client. I tried using fs.stat which provides birthtime but does not include the filename. So, my question is: is birthtime equivalent to the file created date? How can I sen ...

Iterate over an array utilizing the $.getJSON method for data retrieval

I have encountered an issue while using a for loop to iterate through an array of dates in a JSON request. The problem is that the loop seems to be fetching only the first item in the array each time it iterates, as if ignoring the variable i or being cach ...