title: using corona sdk(lua), After moving an object in grid is equal to last object present in the grid, why?
body: <p>I am newbie to corona,started learning with a sample app, Here I am facing a problem, need some suggestion, I am dragging an object to an empty position where it is 2 places after or before or up or down the current object, but when I drag an object the last element in the grid(which is not empty) becomes the same as the dragged object, which is weird error,I don't know how to solve it. Here I am pasting the code, please tell me the solution. Thanks in advance</p>
  
  <p>module(..., package.seeall)
  function new()</p>
  
  <pre><code>local localGroup = display.newGroup()
  
  local gamebg=display.newImageRect("gameBG.png",_W,_H)
  gamebg:setReferencePoint(display.centerReferencePoint)
  gamebg.x=_W/2
  gamebg.y=_H/2
  
  
  
  
  swapButton = {} --A 2D Array
  local instance = {}
  
  require "sprite"
  
  level={ 1,0,1,1,0,1,0,
              1,1,0,1,1,0,1,
      0,1,1,0,0,1,1,
      1,0,1,1,0,1,0,
      1,1,0,1,1,0,1,
      0,1,1,0,0,1,1,
      1,1,0,1,0,0,0}
  
  print(level[2])
  local tempX=130
  local tempY=60
  
  local temp=1;
  
  for i = 0, 6, 1 do
      swapButton[i]={}
      instance[i]={}
  
      for j=0,6,1 do
          print(level[temp+1])
  
          if level[temp]== 1 then
              print("true")
                  instance[i][j]=sprite.newSprite(sprite.newSpriteSet(sprite.newSpriteSheet("fireBall.png",36,36),1,40))
                  instance[i][j].x=tempX
                  instance[i][j].y=tempY
  
  
              instance[i][j].id=math.random ( 49 )
  
              instance[i][j].blendMode="add"
              instance[i][j]:prepare()
              instance[i][j]:play()
  
              --print (math.random (1,49))
  
              function myTouchListener(event)
                  local t = event.target
  
                  if event.phase == "began" then
                              display.getCurrentStage():setFocus( t )
                      --print(display.getCurrentStage( ))
                              print("************* Began*********")
                              print(i,j)
  
  
                      t.isFocus = true
  
                      -- Store initial position
                  --  t.x0 = event.x - t.x
                  --  t.y0 = event.y - t.y
  
                      t.x0 =  instance[i][j].x
                      t.y0 =  instance[i][j].y
  
                      t.x1 = t.x
                      t.y1 = t.y
                      print(t.x0)
                      print(t.y0)
                      print("t.x1=",t.x1)
                      print("t.y1=",t.y1)
  
  
                  elseif event.phase=="moved" then
  
                              print(instance[i][j].x)
                              print(instance[i][j].y)
                              instance[i][j].x=event.x
                              instance[i][j].y=event.y
                              t.x=instance[i][j].x
                              t.y=instance[i][j].y
  
  
              elseif event.phase == "ended" then
                  print(instance[i][j].x)
                      print(instance[i][j].y)
                      print(i,j)
                      --nearMe(t)
                      getFireBall(t,i,j)
  
                              print("Here the 2d array after swap")
                              for l=0,6,1 do
                                  for m=0,6,1 do
                                      print(instance[l][m])
                                      end
  
                                  end
  
                      display.getCurrentStage( ):setFocus( nil );
                      t.isFocus = false
  
                  end
              end
              function getFireBall(t2,setX,setY)
                   print(i,j)
                                       print(setX,setY)
                                      print(math.abs(t2.x-t2.x0))
                                      print(math.abs(t2.y-t2.y0))
                                      if (math.abs(t2.x-t2.x0) &gt; 70 and math.abs(t2.x-t2.x0)&lt;90)  or (math.abs(t2.y-t2.y0)&gt;70 and math.abs(t2.y-t2.y0)&lt;90)then
  
                                              if t2.x&gt;t2.x0 and math.abs(t2.y-t2.y0) &lt;= math.abs(t2.x-t2.x0) then
  
                          print("Forward")
  
                                                      print("it fixes here")
  
                                                      for p=0, 6 , 1 do
                                                          for q=0, 6, 1 do
                                                              if instance[setX][setY]~=nil then
                                                              if (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x&gt;=instance[setX][setY].x-10 and swapButton[p][q].x&lt;=instance[setX][setY].x+10))
                                                                  and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y&gt;=instance[setX][setY].y-10 and swapButton[p][q].y&lt;=instance[setX][setY].y+10))then
  
                                                                  if instance[p][q]==nil then
  
                                                                       print ("placeHere")
                                                                       print(i,j)
                                                                      -- instance[p][q+2]=instance[setX][setY]
                                                                      print(p,q)
                                                                      print(swapButton[p][q].x,swapButton[p][q].y)
                                                                      print(setX,setY)
                                                                      instance[p][q]=sprite.newSprite(sprite.newSpriteSet(sprite.newSpriteSheet("fireBall.png",36,36),1,40))
                                                                       instance[p][q].x=swapButton[p][q].x
                                                                       instance[p][q].y=swapButton[p][q].y
                                                                       instance[p][q].blendMode="add"
                                                                       instance[p][q]:prepare()
                                                                       instance[p][q]:play()
                                                                       instance[p][q]:addEventListener("touch",myTouchListener)
                                                                       instance[setX][setY].x=t2.x0
                                                                       instance[setX][setY].y=t2.y0
                                                                       print(setX,setY)
                                                                      display.remove(instance[setX][setY])
                                                                       instance[setX][setY]=nil
                                                                      display.remove( instance[p][q-1] )
  
                                                                      instance[p][q-1]=nil
                                                                      print(i,j)
                                                                      print(p,q)
                                                                      i=p;
                                                                      j=q;
  
                                                                      print(i,j)
                                                                      print(instance[i][j])
                                                                    --  instance[i][j]=nil
                                                                else 
                                                                    print("fireBall is Exists")
                                                                    instance[i][j].x=t2.x0
                                                                    instance[i][j].y=t2.y0
                                                                  end
                                                              end
                                                          end
                                                          end
                                                      end
  
  
                                              elseif t2.x&lt;t2.x0 and math.abs(t2.y-t2.y0) &lt;= math.abs(t2.x-t2.x0) then
                          print("Backward")
  
                                                      local backward=false
  
                                                      for p=0, 6 , 1 do
                                                          for q=0, 6, 1 do
                                                              print(swapButton[p][q].x)
                                                              print(swapButton[p][q].y)
                                                             print(p,q)
                                                              print(setX,setY)
                                                              print(setX,setY-2)
                                                              if instance[setX][setY]~=nil then
                                                                  if (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x&gt;=instance[setX][setY].x-10 and swapButton[p][q].x&lt;=instance[setX][setY].x+10))
                                                                  and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y&gt;=instance[setX][setY].y-10 and swapButton[p][q].y&lt;=instance[setX][setY].y+10))
                                                                  and backward==false then
                                                                      print(p,q)
  
                                                                  if instance[p][q]==nil then
  
                                                                      print ("placeHere")
  
                                                                      -- instance[p][q+2]=instance[setX][setY]
                                                                      print(p,q)
                                                                      print(swapButton[p][q].x,swapButton[p][q].y)
                                                                      print(setX,setY)
                                                                      instance[p][q]=sprite.newSprite(sprite.newSpriteSet(sprite.newSpriteSheet("fireBall.png",36,36),1,40))
                                                                       instance[p][q].x=swapButton[p][q].x
                                                                       instance[p][q].y=swapButton[p][q].y
                                                                       instance[p][q].blendMode="add"
                                                                       instance[p][q]:prepare()
                                                                       instance[p][q]:play()
                                                                       instance[p][q]:addEventListener("touch",myTouchListener)
                                                                       instance[setX][setY].x=t2.x0
                                                                       instance[setX][setY].y=t2.y0
                                                                       backward=true
                                                                       print(setX,setY)
                                                                       print(p,q)
                                                                        instance[setX][setY]=nil
                                                                       display.remove(instance[setX][setY])
  
  
                                                                      instance[p][q+1]=nil
  
                                                                      display.remove( instance[p][q+1] )
  
                                                                     -- print(i,j)
  
                                                                      i=p;
                                                                      j=q;
                                                                      print(i,j)
  
                                                                else
                                                                      print("Already Exists")
                                                                      instance[setX][setY].x=t2.x0
                                                                      instance[setX][setY].y=t2.y0
  
                                                          end
                                                              end
  
  
                                                  end
                                                  end
  
                                                      end
  
  
                      elseif t2.y&gt;t2.y0 and math.abs(t2.x-t2.x0)&lt;= math.abs(t2.y-t2.y0)then
                          print("Downward")
                                                      local downward=false
  
                                                      for p=0, 6 , 1 do
                                                          for q=0, 6, 1 do
                                                              print(swapButton[p][q].x)
                                                              print(swapButton[p][q].y)
  
  
                                                              if instance[setX][setY]~=nil then
                                                              if (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x&gt;=instance[setX][setY].x-10 and swapButton[p][q].x&lt;=instance[setX][setY].x+10))
                                                                  and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y&gt;=instance[setX][setY].y-10 and swapButton[p][q].y&lt;=instance[setX][setY].y+10))
                                                                  and downward==false then
                                                                 print(p,q)
                                                                  if instance[p][q]==nil then
  
                                                                       print ("placeHere")
  
                                                                      -- instance[p][q+2]=instance[setX][setY]
                                                                      print(p,q)
                                                                      print(swapButton[p][q].x,swapButton[p][q].y)
                                                                      print(setX,setY)
                                                                      instance[p][q]=sprite.newSprite(sprite.newSpriteSet(sprite.newSpriteSheet("fireBall.png",36,36),1,40))
                                                                       instance[p][q].x=swapButton[p][q].x
                                                                       instance[p][q].y=swapButton[p][q].y
                                                                       instance[p][q].blendMode="add"
                                                                       instance[p][q]:prepare()
                                                                       instance[p][q]:play()
                                                                       instance[p][q]:addEventListener("touch",myTouchListener)
                                                                       instance[setX][setY].x=t2.x0
                                                                       instance[setX][setY].y=t2.y0
                                                                       downward=true
                                                                       print(setX,setY)
                                                                       instance[setX][setY]=nil
                                                                       display.remove(instance[setX][setY])
                                                                      instance[p-1][q]=nil
                                                                      display.remove( instance[p-1][q])
  
  
  
                                                                      print(i,j)
                                                                      i=p;
                                                                      j=q;
  
                                                                      print( i,j )
                                                                    --  instance[i][j]=nil
                                                                else 
                                                                    print("fireBall is Exists")
                                                                    print(p,q)
                                                                    instance[i][j].x=t2.x0
                                                                    instance[i][j].y=t2.y0
                                                                  end
                                                              end
                                                          end
                                                          end
                                                      end
  
  
                      elseif t2.y&lt;t2.y0 and math.abs(t2.x-t2.x0)&lt;= math.abs(t2.y-t2.y0)then
                          print("Upward")
                                                 local backward=false
  
                                                      for p=0, 6 , 1 do
                                                          for q=0, 6, 1 do
                                                              print(swapButton[p][q].x)
                                                              print(swapButton[p][q].y)
  
  
                                                              if instance[setX][setY]~=nil then
                                                              if (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x&gt;=instance[setX][setY].x-10 and swapButton[p][q].x&lt;=instance[setX][setY].x+10))
                                                                  and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y&gt;=instance[setX][setY].y-10 and swapButton[p][q].y&lt;=instance[setX][setY].y+10))
                                                                  and backward==false then
                                                                 print(p,q)
                                                                  if instance[p][q]==nil then
  
                                                                       print ("placeHere")
  
                                                                      -- instance[p][q+2]=instance[setX][setY]
                                                                      print(p,q)
                                                                      print(swapButton[p][q].x,swapButton[p][q].y)
                                                                      print(setX,setY)
                                                                      instance[p][q]=sprite.newSprite(sprite.newSpriteSet(sprite.newSpriteSheet("fireBall.png",36,36),1,40))
                                                                       instance[p][q].x=swapButton[p][q].x
                                                                       instance[p][q].y=swapButton[p][q].y
                                                                       instance[p][q].blendMode="add"
                                                                       instance[p][q]:prepare()
                                                                       instance[p][q]:play()
                                                                       instance[p][q]:addEventListener("touch",myTouchListener)
                                                                       instance[setX][setY].x=t2.x0
                                                                       instance[setX][setY].y=t2.y0
                                                                       backward=true
                                                                       print(setX,setY)
                                                                       instance[setX][setY]=nil
                                                                       display.remove(instance[setX][setY])
                                                                       instance[p+1][q]=nil
                                                                      display.remove( instance[p+1][q] )
  
  
  
                                                                      print(i,j)
                                                                      i=p;
                                                                      j=q;
  
                                                                      print(i,j)
                                                                    --  instance[i][j]=nil
                                                                else 
                                                                    print("fireBall is Exists")
                                                                    print(p,q)
                                                                    instance[i][j].x=t2.x0
                                                                    instance[i][j].y=t2.y0
                                                                  end
                                                              end
                                                              end
                                                          end
                                                      end
  
  
                      end
                                          else
  
  
                                                  instance[setX][setY].x=t2.x0
                                                    instance[setX][setY].y=t2.y0
  
  
  
                                          end
  
                                      end
  
          instance[i][j]:addEventListener("touch",myTouchListener)
  
          else 
              print(instance[i][j])
              print(tempX)
              print(tempY)
      --instance[i][j].x=tempX
              --instance[i][j].y=tempY
  
          end
  
  
          swapButton[i][j]= display.newImage("circle.png",40,40)
  
          swapButton[i][j].x=tempX
          swapButton[i][j].y=tempY
          tempX=tempX+40 
         temp=temp+1;
         print(temp)
         print ("swapButtn at"..swapButton[i][j].x)
  
  
      end
      tempX=130
      tempY=tempY+40
  end
  
  localGroup:insert(gamebg)
  
  
  return localGroup
  </code></pre>
  
  <p>end</p>