touchEvent for Grid(2D-Array)of Sprites using Corona sdk

I have a grid of animated sprites in an 2D-Array, but when put the eventListener for a sprite object it is showing the following error "Director ERROR: Failed to load module 'game' - Please check if the file exists and it is correct.", when I remove the code for touch the next scene pops up with animated fireballs in a grid(which is nice), but I wanted to implement touchevents for the individual sprites in the grid. Please help me. Thanks

Here Is the 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"


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

end

thanks

-Hemanth

Answers


You're missing an end statement for your listener and it's implementation itself is iffy because you didn't declare myTouchListener. Try the following:

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

Need Your Help

Django Crispy Forms - Add Button via Helper

django forms button helper django-crispy-forms

I studied the Crispy-Forms documentation and I tried to put an extra button into one of my forms. With

raw_input and dict

python dictionary raw-input

I'm attempting to either put the value from "room" as the key, and "acquity" as the value a dictionary or each time the code runs have the end value "acquity" assigned to a new variable (it needs t...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.