Skip to main content
Participant
November 27, 2015
Answered

Hello there! Swap between multiple objects

  • November 27, 2015
  • 1 reply
  • 466 views

Hi! I need some help understanding the way it can be done.

Lets say I have 2 objects a and b. If I want a to become b and b to become a I guess this is the way is done:

var temp_a:Object = a;

a = b;

b= temp_a;

This works perfectly fine with only 2 objects. The problem is when I am attempting to swap a with b, b with c and c with a.

Could please anyone help me make it through? If it does matter, I am working at a match 3 game where some rows are sliding left or right depending on a certain direction. It can go as a=c,c=b,b=a or a=b,b=c,c=a. I can make the gems swap positions correctly but as Objects, are the same as they were before.

This topic has been closed for replies.
Correct answer nezarov

Or:

var finalRollerTiles:Array = rollerElements;

var lastItem = finalRollerTiles[finalRollerTiles.length-1];

finalRollerTiles.pop();

finalRollerTiles.unshift(lastItem);

1 reply

Inspiring
November 27, 2015

var temp_a:Object = a;

var temp_b:Object = b;

var temp_c:Object = c;

c = temp_b;

b = temp_a;

a = temp_c;

Participant
November 27, 2015

It would be silly of me to ask for help your answer would be the right one. Not that it isn't, but I still can't get it to work.

This would be my approach:

//create array to store tiles that move on grid;

var rollerTiles:Array = new Array();

for (var i:int = 0; i <= gridWidth - 1; i++)

{

     for (var j:int = 0; j <= gridHeight - 1; j++)

     {

          if (tileVector!=null && _gridArray >= 11)

          {

               // tileVector is Object and for each rolling tile I have assigned a int value in this _gridArray.

               //if the in value is above 11 i'm storing my value in a new array.

              rollerTiles.push(_gridArray)

          }

     }

}

// since the above check does not find my values and an ascending order (in matrix), I need to sort them so I can tell wich item goes on wich position.

  rollerTiles.sort(Array.NUMERIC);

  var rollerElements:Array = new Array();

  for ( var m:int = rollerTiles[0]; m <= rollerTiles[rollerTiles.length - 1]; m++)

  {

       for (var a:int = 0; a <= gridWidth - 1; a++)

       {

            for (var b:int = 0; b <= gridHeight - 1; b++)

            {

                 if (tileVector!=null && _gridArray == m)

                 {

                      // now I'm pushing in the same order as the rollerTiles value, my objects into a new array.

                      rollerElements.push(tileVector);             

                 }

            }

       }

  }

// finally here I get to know that rollerElements[0] goes on the rollerElements[1], 1 on 2, 2 on 3, and so on....until the last one goes on 0.

for each rollerElements there is a real object equivalent to "tileVector" mentioned above.

for ( var n:int = 0; n <= rollerElements.length - 1; n++)

{

     if (n + 1 > rollerElements.length - 1)

     {

          TweenMax.to(rollerElements._mc, .2, { x: rollerElements[0]._mc.x , y:  rollerElements[0]._mc.y } );

     }

     else if (n + 1 <= rollerElements.length - 1)

     {

          TweenMax.to(rollerElements._mc, .2, { x: rollerElements[n + 1]._mc.x , y:  rollerElements[n + 1]._mc.y } );

     }

}

// now that i moved them I need to tell them that they have to swap as objects.

//this part I can't get it through no matter what.

//creating an new array of objects, doesn't do the trick do.

var finalRollerTiles:Array = new Array()

finalRollerTiles.push(rollerElements);

var lastItem = finalRollerTiles[finalRollerTiles.length-1]

finalRollerTiles.pop();

finalRollerTiles.unshift(lastItem);


// here my arrays would look like this

//rollerElements, output  ( 0,1,2,3....9)

// finalRollerTiles,output ( 9,0,1,2,....)

// sadly rollerElement[0]= finalRollerTiles[0] doesn't do the trick

Inspiring
November 27, 2015

finalRollerTiles.push(rollerElements);

You're pushing all the array inside another one, so you'll get one element only as an array..

you have to push the elements using for loop:

var finalRollerTiles:Array = new Array();

for (var f:int = 0; f<rollerElements.length; f++)

{

finalRollerTiles.push(rollerElements);

}

var lastItem = finalRollerTiles[finalRollerTiles.length-1];

finalRollerTiles.pop();

finalRollerTiles.unshift(lastItem);