Search code examples
javascriptnode.jsgame-engine

Console.log gives correct output but when accessing set value it gives wrong output


Called Function:

this.findVerticalPossibleScoring = function(){
        var possibilitySet = [];
        for (var j = 0; j < 9;j++ ) {
            for (var i = 0; i < 7; ){
                var tempTile = this._tiles[i][j];
                if(this.gameTilesValue[i][j]!=-1){
                    var tileTagValue = this.gameTilesValue[i][j];
                    if(this.gameTilesValue[i+1][j]==tileTagValue && this.gameTilesValue[i+2][j]==tileTagValue){
                        setElement = [];
                        do{
                            var tempPoint = this.makeArray(i,j);
                            setElement.push(tempPoint);
                            console.log(" verical i:"+i+" j:"+j);
                            i=i+1;
                        }while(i<9&&this.gameTilesValue[i][j]==tileTagValue);
                        possibilitySet.push(setElement);
                        continue;
                    }
                }
                i = i+1;
            }
        }
        return possibilitySet;
    };
    this.makeArray = function (a,b){
        console.log("element i:"+a+" j:"+b);
        var arrayTemp = [];
        arrayTemp.push(a);
        arrayTemp.push(b);
        return arrayTemp;
    };

Calling function part:

if(scoringPossible == true){
                //blast the tiles and add new tiles;
                 var verticalPossibleScoring = this.findVerticalPossibleScoring();
                toBeDeletedTiles = [];
                for(var i=0;i<verticalPossibleScoring.length;i++){
                    var tempSet = verticalPossibleScoring[i];
                    for(var j = 0;j<tempSet.length;j++){
                        var tempSetEntry = tempSet[i];
                        console.log("TILE i:"+tempSetEntry[0]+" j:"+tempSetEntry[1]);
                    }
                }
            }

I have added called function as well as calling function if loop as calling function is too big. I know this is infamous javascript loop issue. I am using gc-devkit game engine which is new and I new to it. I had solved the same issue for UIImage in it by creating custom class, but here I don't require custom array for it. Can any one guide me through this issue. Thanks in advance.


Solution

  • You use j as your loop variable when iterating over tempSet but then use i when getting elements from tempSet. Maybe just change

    var tempSetEntry = tempSet[i];
    

    to

    var tempSetEntry = tempSet[j];