Restting Code- For Newbies


Hello… And alrighty, so the issue I’ve been having is that I can’t seem to figure out how to reset my code but with a certain variable changed after a specific event. For more clarification, I’m trying to reset the code after my character collides with an enemy, but with one less life

Player: function (img, x, y, w, h) {
//bunch of functional code regarding animation, location and other properties goes here and whatnot
this.lives = 3;

^^^Character variable stated in a separate file

if (entity.type === "enemy") {
            if (player.x < entity.x && player.y >= entity.y) {
                player.x = entity.x - player.w;
                player.lives -= 1;
            if (player.x > entity.x && player.y >= entity.y) {
                player.x = entity.x + entity.w;
                player.lives -= 1;

            if (player.y < entity.y && (player.x + player.w) > entity.x + 10 &&
                player.x < (entity.x + entity.w) - 10 && player.velY >= 0) {
                player.y = entity.y - player.h;
                player.velY = 0;
                player.lives -= 1;

^^^This is the already functional collision code/the event which I need to restart my code with…
I’ve tried the window.reload, but that resets everything, and renders the lives useless.
So yeah, any help regarding the above would greatly be appreciated!


Without knowing too much about what you’re doing… Is it possible to just change the position of your current player, to the starting point, and decrement lives?

    player's x to 0,
    player's y to 0,
    lives decreased


The only problem with that is that it doesn’t reset all the other variables which is kinda my main concern, otherwise it would’ve been a great idea. An example being the objects that the character has moved or the score that was obtained.


If you want to do it the slow way… Go through and reset every variable that controls each of them.

objectx = 0;
objecty = 0;
object2x = 0;
object2y = 0;
playerx = 0;
playery = 0;
score = 0;


Thanks, can’t believe I didn’t think of that myself!!!