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