问题
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
回答1:
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
来源:https://stackoverflow.com/questions/12318503/touchevent-for-grid2d-arrayof-sprites-using-corona-sdk