当前位置: 动力学知识库 > 问答 > 编程问答 >

arrays - using corona sdk(lua), After moving an object in grid is equal to last object present in the grid, why?

问题描述:

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

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<t2.x0 and math.abs(t2.y-t2.y0) <= 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>=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 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>t2.y0 and math.abs(t2.x-t2.x0)<= 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>=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 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<t2.y0 and math.abs(t2.x-t2.x0)<= 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>=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 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

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].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 =  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].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


                                                                    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

        instance[i][j]:addEventListener("touch",myTouchListener)


        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

分享给朋友:
您可能感兴趣的文章:
随机阅读: