samedi 24 décembre 2016

How could I optimize this long line of "if" statements?

So, I am making a clicker game using Javascript, and I encountered that I needed to make a LOT, and I'm talking about 300, if statements. I did all the extremely tedious work, and got my game going.

In order to show you the problem I need to show you my game. So basically, everytime I click on a button, one of ten images update.

IMG1

IMG2

The thing is, I made TONS of if statements for each number, and for each material that you can gather.

I was wondering if there was a way to get rid of all these if statements, and have the game work normally, or it there was a way to optimize it.

Code: JSFiddle Most of the code goes like this:

if (A == 18) {
    document.getElementById("A1").src = "images/blocks/2A.png";
    document.getElementById("A2").src = "images/blocks/2A.png";
    document.getElementById("A3").src = "images/blocks/2A.png";
    document.getElementById("A4").src = "images/blocks/2A.png";
    document.getElementById("A5").src = "images/blocks/2A.png";
    document.getElementById("A6").src = "images/blocks/2A.png";
    document.getElementById("A7").src = "images/blocks/2A.png";
    document.getElementById("A8").src = "images/blocks/2A.png";
    document.getElementById("A9").src = "images/blocks/1A.png";
    document.getElementById("A10").src = "images/blocks/1A.png";
}

If you need a link to the actual game, let me know, since JSFiddle doesn't have my pictures.

Aucun commentaire:

Enregistrer un commentaire