0

我是电晕的新手,开始使用示例应用程序学习,在这里我遇到了一个问题,需要一些建议,我将一个对象拖到一个空位置,它位于当前对象之后或之前或上或下 2 个位置,但是当我拖动一个对象时,网格中的最后一个元素(不是空的)变得与拖动的对象相同,这是一个奇怪的错误,我不知道如何解决它。我在这里粘贴代码,请告诉我解决方案。提前致谢

模块(...,package.seeall)函数新()

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

结尾

4

1 回答 1

0

嗨,这是修剪后的代码

模块(...,package.seeall)函数新()

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

结尾

于 2012-09-22T05:33:56.763 回答