What is the best way to cycle through sprite positions within an array?

Having a string array containing the sprites to display:

var lts = [
    "letterA",
    "letterB",
    "letterC",
    "letterD",
    "letterE",
    "letterF"
];

These sprites are loaded in assets.js:

function loadAssets(callback){

    function loadSprite(fileName){
        assetsStillLoading++;

        let spriteImage = new Image();
        spriteImage.src = "assets/sprites/" + fileName;

        spriteImage.onload = function()
        {
            assetsStillLoading--;
        }

        return spriteImage;
    }
    sprites.background = loadSprite('spr_background_pink.png');

    sprites.letterA = loadSprite('/Letters/letter_A.png');
    sprites.letterB = loadSprite('/Letters/letter_B.png');
    sprites.letterC = loadSprite('/Letters/letter_C.png');
    sprites.letterD = loadSprite('/Letters/letter_D.png');
    sprites.letterE = loadSprite('/Letters/letter_E.png');
    sprites.letterF = loadSprite('/Letters/letter_F.png');
}

The goal is to cycle through the letter options and select the correct one, like this:

letterNo = letterNo + 1;

var string = "sprites." + lts[letterNo];
console.log("sprite loading:" + string);
this.sprite = string;

Encountering an issue where it throws an error:

sprites.letterB causing:

TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap.

Seeking assistance on how to resolve this problem effectively.

Answer №1

"sprites.letterB" you will find a string that is not connected to the sprites object in any way. To access a key within the sprites object, be sure to utilize bracket notation: sprites[lts[letterNo]].

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

Guide to sending back a promise using JQuery

Here is the Durendal code I am working with: var variable = ko.observable(""); function activate(){ return myModel.doSomething(variable); } The doSomething function looks like this: function doSomething(variable){ if(someCondition) return ...

Example TypeScript code: Use the following function in Angular 5 to calculate the total by summing up the subtotals. This function multiplies the price by the quantity

I have a table shown in the image. I am looking to create a function that calculates price* quantity = subtotal for each row, and then sum up all the subtotals to get the total amount with Total=Sum(Subtotal). https://i.stack.imgur.com/4JjfL.png This is ...

Adding images to chart labels in vue-chartjs explained

I'm facing a challenge in adding flag icons below the country code labels, and I could really use some guidance. Here is an image of the chart with my current code The flag images I need to use are named BR.svg, FR.svg, and MX.svg, located under @/a ...

Is it feasible to incorporate Mat-Paginator into a standard table within Angular?

I created an HTML table in Angular to display data from an API, and now I'm looking to incorporate a mat-paginator. Below is the code snippet: <table class="table" style="text-align: center;"> <thead class="thead-lig ...

The process of re-rendering a component is facilitated by applying various filters

Filters are an essential feature in my application, as they help limit the output of a list of users. You can see a live example of this concept in action on my codesandbox. The functionality allows users to apply multiple filters to narrow down their sea ...

What could be causing the state to not update as anticipated?

I am currently in the process of developing a TicTacToe game and have a requirement to maintain the current player in state under the name currentPlayer. The idea is that after one player makes a move, I need to update currentPlayer to represent the opposi ...

Adding information to JSON array in AngularJS

I've encountered a challenging scenario where I need to transfer data from one controller to another. My application functions in the following way: View 1 -> retrieves users from a JSON array and displays them in a table. Upon clicking "add use ...

React component not rendering iteration over Set in JSX

I need help figuring out how to iterate through a JavaScript Set() that is being passed down in props within a React component. I am struggling to get anything to display on the screen when attempting to loop through it: {selectedCity.forEach((key, v ...

Discovering the final step of a for loop when working with JavaScript objects

Currently, my data consists of: {12: Object, 13: Object, 14: Object} I need help figuring out how to detect the final iteration in the for loop below: for (var i in objs){ console.log(objs[i]); } Does anyone have any solutions? ...

Enhance the functionality of JavaScript for multiple images using ASP.NET

I have been searching extensively for a way to achieve the following necessary function. Initially, I successfully replicated this function in an asp.net application using VB for a single image. However, I am now attempting to apply it to multiple images. ...

Update the class name by utilizing template literals

I'm currently in the process of mastering template literals as I have a project where I need to utilize them. However, I seem to be encountering an issue that I can't quite figure out. Here is the code that is currently working: const classes = ...

What is the process for combining two geometries or meshes in three.js?

I am working with two types of geometries: a line-based square 2D geometry and an icon image. My goal is to center and place the icons inside the designated region of the 2D rectangle. Here is the code snippet I have written: var combined = new THREE.Ge ...

Switching an element from li to div and vice versa using Jquery Drag and Drop

I am currently experimenting with the following: Stage 1: Making the element draggable from li to div upon dropping into #canvas Placing the draggable element inside #canvas Stage 2: Converting the draggable element from div back to li when dropped i ...

Creating a dynamic dropdown menu in Laravel 5.3 - A step-by-step guide

Here is my HTML code: <div class="form-group has-feedback{{ $errors->has('kdkotama') ? ' has-error' : '' }}"> <select class="form-control" name="kdkotama" id="kdkotama"> <option value="">---- ...

How can I retrieve both the keys and values of $scope in AngularJS?

Here is the code I have written to retrieve all key values using $scope. $scope.Signup={}; $scope.Message=''; $scope.SubmitPhysicianSignup = function() { var url = site_url + 'webservices/physician_signup'; //console.lo ...

reach an agreement on the implementation of infinite scrolling when navigating backwards

I'm looking to create a feature where clicking the back button will return me to the same position on the page. A great example of this is on . If you scroll down, click on a product, and then go back from the product page, you'll be taken back t ...

Aggregate data in Group 2D by a certain column and calculate the total sum of another column to create an associative

I am looking to categorize data from my 2D array based on one column and then calculate the sum of another column within each group. In my dataset, decimal points are represented by commas as these are float values. Input: [ [18, '1,07'], ...

"Implementing a nested drawer and appbar design using Material UI, along with incorporating tabs within

I am currently working on an app that includes tabs for different files, each of which requires its own drawer and appbar. I found a codesandbox example that is similar to what I am trying to implement. Here is the link to the codesandbox One issue I hav ...

Modify class or id of element according to the parent element's class in JavaScript

Is there a way to update the class of a child element based on the parent element's class? For instance <div class="change"> <div class="show-more"></div> </div> If the parent has class="change", then change the child&ap ...

Looking for a solution to dynamically fill a list in JQuery with data from a JSON file. Any suggestions for troubleshooting?

Currently, I am utilizing a JSON file to fetch Quiz questions. In my approach, each question is being stored in an array as an object. The structure of the question object includes 'text' (the actual question), 'choices' (an array of po ...