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

module(..., package.seeall)

function new()

``local localGroup = display.newGroup()local gamebg=display.newImageRect("gameBG.png",_W,_H)gamebg:setReferencePoint(display.centerReferencePoint)gamebg.x=_W/2gamebg.y=_H/2swapButton = {} --A 2D Arraylocal 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=130local tempY=60local temp=1;for i = 0, 6, 1 doswapButton[i]={}instance[i]={}for j=0,6,1 doprint(level[temp+1])if level[temp]== 1 thenprint("true")instance[i][j]=sprite.newSprite(sprite.newSpriteSet(sprite.newSpriteSheet("fireBall.png",36,36),1,40))instance[i][j].x=tempXinstance[i][j].y=tempYinstance[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.targetif event.phase == "began" thendisplay.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.yt.x0 = instance[i][j].xt.y0 = instance[i][j].yt.x1 = t.xt.y1 = t.yprint(t.x0)print(t.y0)print("t.x1=",t.x1)print("t.y1=",t.y1)elseif event.phase=="moved" thenprint(instance[i][j].x)print(instance[i][j].y)instance[i][j].x=event.xinstance[i][j].y=event.yt.x=instance[i][j].xt.y=instance[i][j].yelseif event.phase == "ended" thenprint(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 dofor m=0,6,1 doprint(instance[l][m])endenddisplay.getCurrentStage( ):setFocus( nil );t.isFocus = falseendendfunction 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) > 70 and math.abs(t2.x-t2.x0)<90) or (math.abs(t2.y-t2.y0)>70 and math.abs(t2.y-t2.y0)<90)thenif t2.x>t2.x0 and math.abs(t2.y-t2.y0) <= math.abs(t2.x-t2.x0) thenprint("Forward")print("it fixes here")for p=0, 6 , 1 dofor q=0, 6, 1 doif instance[setX][setY]~=nil thenif (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x>=instance[setX][setY].x-10 and swapButton[p][q].x<=instance[setX][setY].x+10))and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y>=instance[setX][setY].y-10 and swapButton[p][q].y<=instance[setX][setY].y+10))thenif instance[p][q]==nil thenprint ("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].xinstance[p][q].y=swapButton[p][q].yinstance[p][q].blendMode="add"instance[p][q]:prepare()instance[p][q]:play()instance[p][q]:addEventListener("touch",myTouchListener)instance[setX][setY].x=t2.x0instance[setX][setY].y=t2.y0print(setX,setY)display.remove(instance[setX][setY])instance[setX][setY]=nildisplay.remove( instance[p][q-1] )instance[p][q-1]=nilprint(i,j)print(p,q)i=p;j=q;print(i,j)print(instance[i][j])-- instance[i][j]=nilelseprint("fireBall is Exists")instance[i][j].x=t2.x0instance[i][j].y=t2.y0endendendendendelseif t2.x<t2.x0 and math.abs(t2.y-t2.y0) <= math.abs(t2.x-t2.x0) thenprint("Backward")local backward=falsefor p=0, 6 , 1 dofor q=0, 6, 1 doprint(swapButton[p][q].x)print(swapButton[p][q].y)print(p,q)print(setX,setY)print(setX,setY-2)if instance[setX][setY]~=nil thenif (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x>=instance[setX][setY].x-10 and swapButton[p][q].x<=instance[setX][setY].x+10))and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y>=instance[setX][setY].y-10 and swapButton[p][q].y<=instance[setX][setY].y+10))and backward==false thenprint(p,q)if instance[p][q]==nil thenprint ("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].xinstance[p][q].y=swapButton[p][q].yinstance[p][q].blendMode="add"instance[p][q]:prepare()instance[p][q]:play()instance[p][q]:addEventListener("touch",myTouchListener)instance[setX][setY].x=t2.x0instance[setX][setY].y=t2.y0backward=trueprint(setX,setY)print(p,q)instance[setX][setY]=nildisplay.remove(instance[setX][setY])instance[p][q+1]=nildisplay.remove( instance[p][q+1] )-- print(i,j)i=p;j=q;print(i,j)elseprint("Already Exists")instance[setX][setY].x=t2.x0instance[setX][setY].y=t2.y0endendendendendelseif t2.y>t2.y0 and math.abs(t2.x-t2.x0)<= math.abs(t2.y-t2.y0)thenprint("Downward")local downward=falsefor p=0, 6 , 1 dofor q=0, 6, 1 doprint(swapButton[p][q].x)print(swapButton[p][q].y)if instance[setX][setY]~=nil thenif (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x>=instance[setX][setY].x-10 and swapButton[p][q].x<=instance[setX][setY].x+10))and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y>=instance[setX][setY].y-10 and swapButton[p][q].y<=instance[setX][setY].y+10))and downward==false thenprint(p,q)if instance[p][q]==nil thenprint ("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].xinstance[p][q].y=swapButton[p][q].yinstance[p][q].blendMode="add"instance[p][q]:prepare()instance[p][q]:play()instance[p][q]:addEventListener("touch",myTouchListener)instance[setX][setY].x=t2.x0instance[setX][setY].y=t2.y0downward=trueprint(setX,setY)instance[setX][setY]=nildisplay.remove(instance[setX][setY])instance[p-1][q]=nildisplay.remove( instance[p-1][q])print(i,j)i=p;j=q;print( i,j )-- instance[i][j]=nilelseprint("fireBall is Exists")print(p,q)instance[i][j].x=t2.x0instance[i][j].y=t2.y0endendendendendelseif t2.y<t2.y0 and math.abs(t2.x-t2.x0)<= math.abs(t2.y-t2.y0)thenprint("Upward")local backward=falsefor p=0, 6 , 1 dofor q=0, 6, 1 doprint(swapButton[p][q].x)print(swapButton[p][q].y)if instance[setX][setY]~=nil thenif (swapButton[p][q].x==instance[setX][setY].x or (swapButton[p][q].x>=instance[setX][setY].x-10 and swapButton[p][q].x<=instance[setX][setY].x+10))and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y>=instance[setX][setY].y-10 and swapButton[p][q].y<=instance[setX][setY].y+10))and backward==false thenprint(p,q)if instance[p][q]==nil thenprint ("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].xinstance[p][q].y=swapButton[p][q].yinstance[p][q].blendMode="add"instance[p][q]:prepare()instance[p][q]:play()instance[p][q]:addEventListener("touch",myTouchListener)instance[setX][setY].x=t2.x0instance[setX][setY].y=t2.y0backward=trueprint(setX,setY)instance[setX][setY]=nildisplay.remove(instance[setX][setY])instance[p+1][q]=nildisplay.remove( instance[p+1][q] )print(i,j)i=p;j=q;print(i,j)-- instance[i][j]=nilelseprint("fireBall is Exists")print(p,q)instance[i][j].x=t2.x0instance[i][j].y=t2.y0endendendendendendelseinstance[setX][setY].x=t2.x0instance[setX][setY].y=t2.y0endendinstance[i][j]:addEventListener("touch",myTouchListener)elseprint(instance[i][j])print(tempX)print(tempY)--instance[i][j].x=tempX--instance[i][j].y=tempYendswapButton[i][j]= display.newImage("circle.png",40,40)swapButton[i][j].x=tempXswapButton[i][j].y=tempYtempX=tempX+40temp=temp+1;print(temp)print ("swapButtn at"..swapButton[i][j].x)endtempX=130tempY=tempY+40endlocalGroup:insert(gamebg)return localGroup``

end

Hi here is the trimmed code

module(..., package.seeall) function new()

``````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}

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

if level[temp]== 1 then

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]: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 =  instance[i][j].x
t.y0 =  instance[i][j].y

t.x1 = t.x
t.y1 = t.y

elseif event.phase=="moved" then

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(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)

if (math.abs(t2.x-t2.x0) > 70 and math.abs(t2.x-t2.x0)<90)  or (math.abs(t2.y-t2.y0)>70 and math.abs(t2.y-t2.y0)<90)then

if t2.x>t2.x0 and math.abs(t2.y-t2.y0) <= 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>=instance[setX][setY].x-10 and swapButton[p][q].x<=instance[setX][setY].x+10)) and (swapButton[p][q].y==instance[setX][setY].y or (swapButton[p][q].y>=instance[setX][setY].y-10 and swapButton[p][q].y<=instance[setX][setY].y+10))then

if instance[p][q]==nil then

[q+2]=instance[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]:prepare()
instance[p][q]:play()
instance[setX][setY].x=t2.x0
instance[setX][setY].y=t2.y0

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])

else
print("fireBall is Exists")
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

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;

end
tempX=130
tempY=tempY+40
end

localGroup:insert(gamebg)

return localGroup
``````

end

Top