Not working Constant?

Hello Everybody,:beer2:

I´m very confused with this peace of code :expressionless: During the runtime It somehow changed content of constant i have created.

here is fla:

var _currentBlock:Tetris_Shape;
trace(Tetris_Shape._POS_IN_GRID); **[COLOR=Red]//output 3,1,3,2,3,3,3,4,3,1,3,2,3,3,2,3,2,1,2,2,2,3,3,3,3,3,3,2,2,2,2,3,2,3,3,3,3,2,2,4,[COLOR=RoyalBlue]2,3,3,3,4,3,3,2,[/COLOR]3,3,2,3,2,2,1,2[/COLOR]**
var someArray:Array = Tetris_Shape.rewriteBlockData(5, Tetris_Shape._POS_IN_GRID);

trace(Tetris_Shape._POS_IN_GRID);**[COLOR=Red] //output 3,1,3,2,3,3,3,4,3,1,3,2,3,3,2,3,2,1,2,2,2,3,3,3,3,3,3,2,2,2,2,3,2,3,3,3,3,2,2,4,[COLOR=RoyalBlue]2,2,2,3,2,4,3,3,[/COLOR]3,3,2,3,2,2,1,2[/COLOR]**

and here is as:

package {
    
    public class Tetris_Shape {

        public static const _POS_IN_GRID:Array = new Array(
                                                    [[3,1],[3,2],[3,3],[3,4]],
                                                    [[3,1],[3,2],[3,3],[2,3]],
                                                    [[2,1],[2,2],[2,3],[3,3]],
                                                    [[3,3],[3,2],[2,2],[2,3]],
                                                    [[2,3],[3,3],[3,2],[2,4]],
                                                    [[2,3],[3,3],[4,3],[3,2]],
                                                    [[3,3],[2,3],[2,2],[1,2]]
                                                    );

        public static function rewriteBlockData (number:uint, carrier:Array):Array {
            var mirroredNumbers:Array = new Array(5,4,3,2,1,0);
            var easyNum:uint;
            for(var i:uint = 0; i<4; i++){
                easyNum = carrier[number]*[0];
                carrier[number]*[0] = mirroredNumbers[carrier[number]*[1]];
                carrier[number]*[1] = easyNum;
            }
            return carrier;
        }
    }
}

Thank you for eventually responses >:)

That’s pretty bizarre that it changes the constant instead of throwing an error.

var someArray:Array = Tetris_Shape.rewriteBlockData(5, Tetris_Shape._POS_IN_GRID.concat());

would solve the problem tho…

const only applies to the value of _POS_IN_GRID, not any of its properties

Thank you, but it didn´t helped. :slight_smile:

Thank you very much, but don´t you know, how is it even possible that this property was changed … I haven´t done anything to change this property. :slight_smile:

When you use const, you’re saying that no one can change the value of that property for that object. In your case:

var _currentBlock:Tetris_Shape;
Tetris_Shape._POS_IN_GRID = new Array(); // ERROR, const enforced!

It has no way of affecting or recognizing any of the values within that value. If you wanted every one of those array values to be const, you would have to change that object from an array to a custom class and define each one of those values in a new const property.

Make sense?

change to this then;


        public static function rewriteBlockData (number:uint, xcarrier:Array):Array {
            var carrier:Array = xcarrier.concat();
            var mirroredNumbers:Array = new Array(5,4,3,2,1,0);
            var easyNum:uint;
            for(var i:uint = 0; i<4; i++){
                easyNum = carrier[number]*[0];
                carrier[number]*[0] = mirroredNumbers[carrier[number]*[1]];
                carrier[number]*[1] = easyNum;
            }
            return carrier;
        }

Yes it does,

Thank you :slight_smile:

[COLOR=“Black”]sekasi:[/COLOR]

Strange, but it dosen´t work either.

I have another question :slight_smile:

Do you know, why _POS_IN_GRID was changed, or what changed it ?

As for the problem itself, const isn’t what you really need. What you need (int he context of your current code) is a deep copy of your array. The easiest solution for that might be changing _POS_IN_GRID to a method that returns that array where each time it’s called, a new instance of that array is returned…


        public static function _POS_IN_GRID():Array { return new Array(
                                                    [[3,1],[3,2],[3,3],[3,4]],
                                                    [[3,1],[3,2],[3,3],[2,3]],
                                                    [[2,1],[2,2],[2,3],[3,3]],
                                                    [[3,3],[3,2],[2,2],[2,3]],
                                                    [[2,3],[3,3],[3,2],[2,4]],
                                                    [[2,3],[3,3],[4,3],[3,2]],
                                                    [[3,3],[2,3],[2,2],[1,2]]
                                                    );}

I haven’t examined the code too thoroughly but that should probably do it.

[QUOTE=Dart;2356233]I have another question :slight_smile:

Do you know, why _POS_IN_GRID was changed, or what changed it ?[/QUOTE]

rewriteBlockData changed it. You passed it in to rewriteBlockData as carrier and that function is adding values to it.

sekasi started off with the right solution, trying to make a copy of the array through xcarrier.concat(); The problem with that is that it is not a deep copy - or a copy that copies the array and creates copies of all the objects in the array. Instead, you get a copy of the main array, but the reference values it contains references the same nested arrays as the original. Now, you could loop through and copy each one of those over again with another concat(), but that could be a bit of a pain. Just recreating the array from scratch with the method I suggested earlier might be all you need since it creates a completely new array for each use.