I've been working on a library in JavaScript using p5.js for sprite rendering. I've got an object for storing sprite information which is then added to an array that has a function that updates every frame.
function createsprite(img, x, y, width, height, layer, tag){
var sprite = {
img: img,
xpos: x,
ypos: y,
width: width,
height: height,
id: spriteid,
clayer: layer,
stag: tag,
removesprite: removeobject(this.id),
frozen: false
};
spriteid++;
spritesarray.push(sprite)
return sprite;
}
Then I am using this function to draw the sprites, which are added to the array in the function above.
function drawsprites(){
for(let i = 0; i < spritesarray.length; i++){
image(spritesarray[i].img, spritesarray[i].xpos, spritesarray[i].ypos)
}
}
My issue is that I am trying to modify the xpos
of the object but it is only stored temporarily, then it resets to default. I am not sure why this occurs: I am attempting to modify the property of the object but, when the array is drawn with the drawsprites()
function, any modification I made to sprite1
is reset to default.
function draw() {
background(220);
sprite1 = createsprite(img, 0,0,0,0, 1, "Player")
if(keyIsDown(DOWN_ARROW)){
sprite1.ypos -= 10;
}
startnumber++;
drawsprites();
}