Are instance prototypes exhibiting static behavior?

I'm in the process of creating a game for Windows 8 using HTML/Javascript and WinJS, but I'm running into issues with "prototypes" acting statically when they shouldn't. This is my first time working extensively with Javascript and dealing with multiple objects, so I may have made a simple mistake that I can't seem to understand.

Here's a snippet of the code I'm referring to (it's quite lengthy):

(function () {
    "use strict";

    WinJS.Namespace.define("Race", {
        Car: WinJS.Class.derive(
            Race.Common,
            function (slotNum, slotData, canvas, stage, preload, pixelScale) {
                this.canvas = canvas;
                this.stage = stage;
                this.preload = preload;
                this.pixelScale = pixelScale;

                this.slotNum = slotNum;
                this.slotData = slotData;
            },
            {
                slotNum: 0,
                slotData: {},
                position: { x: 0, y: 0, r: 0 },
                lastPosition: { x: 0, y: 0, r: 0 },

                calcRotation: function () {
                    this.lastPosition.r = this.position.r;
                    this.position.r = this.getAngle(this.lastPosition.x, this.lastPosition.y, this.position.x, this.position.y);
                },
            },
            {
                showDebug: true,
                debugCarColors: ['red', 'yellow', 'blue', 'green'],
            }
        )
    });
}());

There are four instances of Race.Car, each with different slotData sets. Each instance has its own slotData and slotNum, confirmed through breakpoints. Position tracking works correctly, but the issue lies with lastPosition. Instead of being set to the previous car's lastPosition, it's being set to the last car's lastPosition. I expected it to be specific to each car based on where I defined it and used this.lastPosition.

Why isn't it behaving as expected and what am I missing? Thank you

Answer №1

When creating an instance of the object, consider including it as a default parameter in the constructor, shown below:

function (slotNum, slotData, canvas, stage, preload, pixelScale) {
            this.canvas = canvas;
            this.stage = stage;
            this.preload = preload;
            this.pixelScale = pixelScale;

            this.slotNum = slotNum;
            this.slotData = slotData;
            this.lastPosition = { x: 0, y: 0, r: 0 };
        },

Cheers :-)

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

Is there a way to determine the names of the functions that are being called?

I'm working on mastering Google Development Tools. Is there a way to determine which specific functions, especially those in Javascript, are needed before a page can load successfully? ...

What is the best way to incorporate a button for toggling CSS animations?

I need help adding a button to toggle the animation on this JSFiddle. I tried adding the code below but can't seem to start and stop the animation. body .a [class^="b"] { width: 200px; height: 142.8571428571px; margin: 0 auto; ...

What is the best way to retrieve all information from GitLab API using Axios?

I am looking to retrieve data from all the projects stored on my GitLab server. As I understand, GitLab usually displays a default of 20 projects per page, so I need to adjust the setting to show more projects at once: https://gitlab-repo.com/api/v4/proje ...

Establishing a Connection with Node/Express Routing via JavaScript

When developing a web application using HTML, JavaScript, and Node.js with Express, I often find the need to navigate between pages based on user actions. In HTML, one approach is to add an href link and then set up routes in my app.js file to handle the ...

Transferring a JavaScript Value to a PHP Variable (Under Certain Constraints)

Although I have reviewed existing questions and answers related to this issue, I found that the solutions provided do not suit my specific situation. Discussing the differences between client-side JavaScript and server-side PHP, two common solutions are: ...

Encountering an issue when running npm build on server

I attempted to deploy my application on the hosting server and encountered the following error: Failed to compile. ./node_modules/next/dist/pages/_app.js Error: failed to process The global thread pool has not been initialized.: ThreadPoolBuildError { kin ...

Having difficulty locating audio files within a Next.js project

I am facing challenges when trying to import my audio files into a redux slice. Currently, I am attempting to retrieve my audio files from the app > public > audio directory. I have made efforts to access my audio files through 2 different director ...

Managing various names of child objects in a recursive manner

I'm currently facing a challenge while working on a nested JSON feed using D3.js Although my code is functioning properly when the child object is named children, I am interested in displaying nodes for other objects as well, not just the ones labele ...

How to prevent click events and still enable scrolling within a DIV using CSS/JS

Basically, I am looking to enable scrolling within a DIV while also disabling click events. Any suggestions on how this can be achieved? Appreciate any help! ...

Creating a feature that uses a button press to initiate an Ajax request based on a specific identifier

I'm looking for the most efficient way to structure a page that involves making Ajax calls. My main issue lies in setting up the event binding for when a user clicks a button. I'm struggling to find a method to pass the ID to the function that t ...

Issues have been encountered with activating checkboxes on Internet Explorer while utilizing ASP.NET CheckBox controls

I'm facing an issue with an HTML form that includes two disabled checkboxes and an image with an onclick event intended to display a popup and enable the checkboxes: <input id="chk1" type="checkbox" disabled="disabled" /> <input id="chk2" ty ...

Learn how to efficiently transfer row data or an array object to a component within Angular by utilizing the MatDialog feature

My goal is to create a functionality where clicking on a button within a specific row opens up a matDialog box displaying all the contents of that row. Below is the HTML snippet: <tr *ngFor="let u of users"> <td data-label="ID& ...

How can values be extracted from a multi-dimensional array using JSON.parse()?

Here is a JSON array before using JSON.parse: var temp = { "queries": [ { "sample_size": 3, "results": [ { "name": "temperature", "tags": { "Tag": [ "temperature" ] ...

Employ the 'this' attribute within _.map

I am facing an issue where I need to call a setState function within a _.map loop in React. However, the loop is losing the reference to 'this' and therefore I cannot use setState as this becomes undefined. cargaDinamica(){ _.map(this.stat ...

Utilize React's Context Provider to centrally manage all state while incorporating async calls

I am currently exploring more refined methods to establish a provider/consumer setup in which an asynchronous call is initiated from the provider, but the consumer does not need to handle state management. Within my context provider, I am fetching data to ...

Unable to showcase Mysql search outcomes in a distinct div section

I have been working on a project to showcase MySQL results by utilizing three different sections/divisions. In Division 1, there are radio buttons that allow for selecting and viewing the results based on different criteria. Division 2 contains text indica ...

What is the best way to retrieve the transpiled string from babel-core?

I've been attempting to utilize babel with npm and it seems like the necessary package is babel-core. My goal is to provide it with a string of ES6 code and receive a transpiled code string in return. It sounds simple enough, but I'm having troub ...

Is there a way to add additional text to a text element within an SVG?

Is it possible to append a new text element at the end of the data label by clicking on that particular text? I have attempted to implement this in my code but the additional text is not being displayed as expected: circleGroup.selectAll("text") ...

When the user clicks, position a div directly below another div

Can anyone help me figure out how to center a div underneath another one when a specific div is clicked using programming? I've tried multiple solutions but nothing seems to work. Below is the code snippet that I have written. You can also check out t ...

What are the steps to ensure my MERN application refreshes properly when deployed on Heroku?

After successfully deploying my MERN app to Heroku, I encountered an issue where pages would come up blank when refreshed. Despite being able to navigate between pages using the nav bar without any errors, a page refresh renders them empty. This troublin ...