I use this constructor to make objects for a game.
Right now, the series of images gets overwritten everytime, such that all the objects look the same on screen.
Here’s the object in question:
function Box() {
this.ready = false;
this.pics = pictures;//[];
this.state = 0;
this.x = 0;
this.y = 0;
this.w = 1;
this.h = 1;
this.fill = "#444";
this.load = function(array){
var foo = [];
pictures = [];
for(var i = 0; i < array.length; i++){
pictures.push(loadPic(array[i]));
foo.push(loadPic(array[i]));
}
//this.pics = pictures;
this.pics = foo;
}
}
The line
this.pics = foo;
seems to do nothing at all.
Also, if I change the initial value of pics to anything but “pictures” (which is a global variable) the game does not start.
Contex: https://github.com/kaninepete/Javascript-Games/blob/images/MVP.js
Initially, you are creating your
playerandtargetobjects and load their pictures by callingload. Because you are using the object-dot-method syntax, for exampleplayer.loadto call the functions all references tothisshould be set as required/expected.However to start the game, you are calling
reset(for example frommyDown). Withinresetthe objects inplayerandtargetare replaced with new instances (notice the calls toaddRect). The methodloadis never called on the new instances and thus both are left with the identical pictures (the globalpicturesas initially set inBox). Because both are sharing the same array,pictures, they end up being painted the same.General advice: I suggest to get rid of the global variables because their dependencies on each other are not obvious. Then you should call the
resetfunction for initialization instead of doubling the implementation on the global level. Try painting a static picture first before introducing event handlers.