0

Got an issue with some function i'm writing. My "bateau" is an object that has a x position and y position to place it inside my "grille" object which is a table of "bateau" elements.

Joueur.prototype.placeFullBateau = function(grille, bateau) {
    var x           = bateau.positionX;
    var y           = bateau.positionY;
    var direction   = bateau.direction;
    if (direction == "droite") {
        for (var i = 0; i <= bateau.taille-1; i++) {
            grille.plateau[x][y+i] = $(bateau);
            console.log(bateau);
            console.log($(bateau));
            console.log("Bateau : "+$(bateau).get(0).nom+", positionY : "+grille.plateau[x][y+i].get(0).positionY);
            console.log(grille.plateau[x][y+i]);
            $(bateau).get(0).positionY += 1;
            };
};

In the console.log() of "bateau", my positionX & positionY are good, they change as i want them to change (i'm showing only two lines, this "bateau" takes 5) :

Object { nom: "Porte-avion", taille: 5, positionX: 4, positionY: 2, direction: "droite" } test.js:100:5

Object { nom: "Porte-avion", taille: 5, positionX: 4, positionY: 3, direction: "droite" } test.js:100:5

In the console.log() of "$(bateau)", the positionY (or positionX, depending on the one i want to vary) remembers the last value it has taken. Here, positionY will be always 7 (cuz it starts at 2 and size is 5).

I'm trying to :

grille.plateau[x][y+i] = $(bateau);
grille.plateau[x][y+i].get(0).positionY = bateau.positionY;
$(bateau).get(0).positionY += 1;

console results

I'm lost.. Hopefully I gave enough informations to get some help.. I wouldn't paste the whole code :/.

Here's a fiddle : https://jsfiddle.net/0jaL7svo/ Not really working, missing images that shows off when i click on a cell. What i'm doing is when i click on a cell, i take its id. Problem is, the id of some cells (which has an object in it) is wrong because it takes the bateau.positionX & bateau.positionY & bateau.numJoueur. (Looks like "441", for the cell (4,4) of the player 1.) Here's the full working project : (see comment below, cannot post two links). Just unzip and load locally (linux filesystem).

Cheers Krach

Krach
  • 3
  • 3
  • 1
    Could you add a working example of the code in http://jsfiddle.net. It's hard to follow what is happening/what you want to happen from the written description alone. – Rory McCrossan Feb 17 '16 at 11:31
  • Why would you ever need to wrap the object in `$()` in the first place? Not at all clear what you are trying to do – charlietfl Feb 17 '16 at 12:01
  • Here : https://jsfiddle.net/0jaL7svo/ It's not really working because i'm using images when i click on a 'td' that changes its background. – Krach Feb 17 '16 at 12:21
  • Full project zip : http://www.filedropper.com/test_182 – Krach Feb 17 '16 at 12:28

1 Answers1

0

I have suggested this be closed as a dupe of this question:

JavaScript closure inside loops – simple practical example

But here's the rationale why:

This is the very common JavasScript closure issue inside a for loop. You need to capture the value of i at the current pass through the loop, not just reference i:

    for (var i = 0; i <= bateau.taille-1; i++) {
            (function(i) {
              grille.plateau[x][y+i] = $(bateau);
              console.log(bateau);
              console.log($(bateau));
              console.log("Bateau : "+$(bateau).get(0).nom+", positionY : "+grille.plateau[x][y+i].get(0).positionY);
              console.log(grille.plateau[x][y+i]);
              $(bateau).get(0).positionY += 1;
            }(i));
    };
Community
  • 1
  • 1
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • Thank you for answering. This is not working. No errors whatsoever but i still get the same result. – Krach Feb 17 '16 at 12:35