使用Corona sdk的精灵网格(2D阵列)的touchEvent

时间:2012-09-07 12:50:52

标签: arrays events touch corona sprite

我在2D数组中有一个动画精灵网格,但是当为一个精灵对象放入eventListener时,它显示以下错误“导演错误:无法加载模块'游戏' - 请检查文件是否存在,这是正确的。“,当我删除触摸代码时,下一个场景弹出一个网格中的动画火球(这很好),但我想为网格中的各个精灵实现touchevents。请帮我。感谢

这是守则:

模块(...,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"


local tempX=130
local tempY=60

for i = 0, 6, 1 do
    swapButton[i]={}
    instance[i]={}

    for j=0,6,1 do      
        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]:prepare()
        instance[i][j]:play()

        instance[i][j]:addEventListener("touch",myTouchListener)
        swapButton[i][j]= display.newImage("circle.png",40,40)
        swapButton[i][j].x=tempX
        swapButton[i][j].y=tempY
        tempX=tempX+40


    end
    tempX=130
    tempY=tempY+40
end

function myTouchListener:touch(event)
if event.phase == "began" then
    display.getCurrentStage( ):setFocus( event.target );
    print(display.getCurrentStage( ))
 elseif event.phase=="moved" then
 print("moved")   
elseif event.phase == "ended" then
    display.getCurrentStage( ):setFocus( nil );
end



localGroup:insert(gamebg)


return localGroup

感谢

-Hemanth

1 个答案:

答案 0 :(得分:0)

你缺少一个监听器的结束语句,它的实现本身就是iffy,因为你没有声明myTouchListener。请尝试以下方法:

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"


    local tempX=130
    local tempY=60

    for i = 0, 6, 1 do
        swapButton[i]={}
        instance[i]={}

        for j=0,6,1 do
            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]:prepare()
            instance[i][j]:play()

            function myTouchListener(event)
                if event.phase == "began" then
                    display.getCurrentStage( ):setFocus( event.target );
                    print(display.getCurrentStage( ))
                elseif event.phase=="moved" then
                    print("moved")
                elseif event.phase == "ended" then
                    display.getCurrentStage( ):setFocus( nil );
                end
            end

            instance[i][j]:addEventListener("touch",myTouchListener)
            swapButton[i][j]= display.newImage("circle.png",40,40)
            swapButton[i][j].x=tempX
            swapButton[i][j].y=tempY
            tempX=tempX+40
        end
        tempX=130
        tempY=tempY+40
    end

    localGroup:insert(gamebg)


    return localGroup
end