Hi everybody,
i would to know how to assign an eventlistener on a table ? is it possible ? if no what’s the solution for do this ?
thanks for you response .
Hi everybody,
i would to know how to assign an eventlistener on a table ? is it possible ? if no what’s the solution for do this ?
thanks for you response .
Hi.
You can do this:
local myTable = {} myTable.touch = function( self, event ) end Runtime:addEventListener( "touch", myTable )
, but not this:
local myTable = {} myTable.touch = function( self, event ) end myTable:addEventListener( "touch", myTable )
However, if you need to re-direct touch processing for an object to any single object (or table), you can do this:
local myTable = {} myTable.touch = function( self, event ) -- self is table -- event.target is circle you touched end local c1 = display.newCircle( 10, 10, 10 ) local c2 = display.newCircle( 20, 20, 10 ) c1:addEventListener( "touch", myTable ) -- c1 catches touch and send processing to myTable.touch c2:addEventListener( "touch", myTable ) -- same thing for c2
hi, thanks for the reply. i tried what you expected but it’s return that the eventlistener attempt a nil value…
do you know why ?
local myGrid = {} local row = 10 local cell = 15 local rectx = 30 local recty = 30 local gridscaley = 0.35 local gridscalex = 0.35 local space = 2 local spaceleft =10 local spaceright = 50 local image = "myx.png" for i = 1, cell do myGrid[i] = {}; for k = 1, row do myGrid[i][k] = display.newImage(image) myGrid[i][k].yScale=0.35 myGrid[i][k].xScale=0.35 myGrid[i][k].alpha = 1 myGrid[i][k].x = (k - 1) \* (rectx + space) + spaceleft myGrid[i][k].y = (i - 1) \* (recty + space) + spaceright end end myGrid.touch = function ( self, event ) end Runtime:addEventListner( "touch", myGrid )
Are you sure that’s the code you have? If so, the last line has a typo:
Runtime:addEventListner
should be
Runtime:addEventListener
Please try this exact code:
local myTable = {} myTable.touch = function( self, event ) print( self,event.x,event.y) end Runtime:addEventListener( "touch", myTable )
Advice -
Don’t forget to look for ‘typos’ in forums answers. I (and others) often type code without running it. Our answers are right, but may contain typos. A common one is the ‘addEventListner’ typo (missing e).
Actually you did do the right case, I corrected my comment above. Sorry I was in a rush.
sorry for the stupid fault … it would spell checker code in my sublim-text
I receive many details of the grillle I touch with this code but what I would like instead is to receive such
grid[2][5] touched --so that I can do transition.to (character, x = grid[2][5].x, y=grid[2][5].y....)
is that possible?
Sure you can do ‘something’ like that, but keeping your objects in a table to track their <x,y> position isn’t necessary. You’ve already got the <x,y> position of the object stored in the object itself, so have the touch move the object ‘character’ to its position.
Try this small snippet:
local character = display.newCircle( 160, 240, 30 ) character:setFillColor(0,1,0) local onTouch = function( self, event ) if(event.phase == "began") then transition.to( character, { self.x, self.y, time = 1000 } ) end return true end local function makeGridObject( group, x, y, color ) group = group or display.currentStage -- allows this call: makeGridObject( nil, 10, 10, {1,1,1} ) local tmp = display.newCircle( group, x, y, 30 ) tmp:setFillColor(unpack(color)) tmp.touch = onTouch tmp:addEventListener( "touch" ) end local objs = display.newGroup() makeGridObject( objs, 10, 10, {1,1,0} ) makeGridObject( objs, 50, 50, {1,0,1} ) makeGridObject( objs, 200, 200, {0,1,1} )
Corrected typos.
hi, thanks for the code. I have changed some parts (x= self.x, etc), but there is a strange thing : apparently the character doesn’t make the transition exactly a the center. sometimes it’s good and sometimes not. I have put a print self.x and self.y and the value are constantly, so it must move perfectly a the center…Do you know why ?
local character = display.newCircle( 160, 240, 30 ) character:setFillColor(1,0,0) local onTouch = function( self, event ) if(event.phase == "began") then print ( self.x, self.y ) transition.to( character, { x=self.x, y=self.y, time = 1000 } ) end return true end local function makeGridObject( group, x, y, color ) group = group or display.currentStage -- allows this call: makeGridObject( nil, 10, 10, {1,1,1} ) local tmp = display.newCircle( group, x, y, 20 ) tmp:setFillColor(unpack(color)) tmp.touch = onTouch tmp:addEventListener( "touch" ) end local objs = display.newGroup() makeGridObject( objs, 10, 10, {1,1,0} ) makeGridObject( objs, 50, 50, {1,0,1} ) makeGridObject( objs, 200, 200, {0,1,1} )
Try this code change to see what happens:
local onTouch = function( self, event ) if(event.phase == "began") then print ( self.x, self.y ) transition.to( character, { x=self.x, y=self.y, time = 1000 } ) print("Moving character to ", self.x, self.y ) timer.peformWithDelay( 1500, function() print("character at", character.x, character.y ) end ) end return true end
hi, its always the same.
and yet your code is super logic … I do not understand why it no longer bug
Sorry, are you saying it still fails for you? I see from your image that it looks wrong.
Here is some modified code (uses a side-effect of physics rendering code to show the center of the object):
local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode( "hybrid" ) local character = display.newCircle( 160, 240, 30 ) character:setFillColor(1,0,0) physics.addBody( character, { radius = 30, isSensor = true } ) local onTouch = function( self, event ) if(event.phase == "began") then print ( self.x, self.y ) transition.to( character, { x=self.x, y=self.y, time = 1000 } ) print("Moving character to ", self.x, self.y ) timer.performWithDelay( 1500, function() print("character at", character.x, character.y ) end ) end return true end local function makeGridObject( group, x, y, color ) group = group or display.currentStage -- allows this call: makeGridObject( nil, 10, 10, {1,1,1} ) local tmp = display.newCircle( group, x, y, 20 ) tmp:setFillColor(unpack(color)) tmp.touch = onTouch tmp:addEventListener( "touch" ) physics.addBody( tmp, { radius = 20 } ) end local objs = display.newGroup() makeGridObject( objs, 10, 10, {1,1,0} ) makeGridObject( objs, 50, 50, {1,0,1} ) makeGridObject( objs, 200, 200, {0,1,1} )
Please note, this will only work like this for recent Corona builds (I’m using 2014.2370), so if you’re using an old one it may not behave as in this video (watch in HD full screen to see the centers):
https://www.youtube.com/watch?v=34lffgSDlfo&feature=youtu.be&hd=1
Whatever the case, if you get misaligned transitions and one of the following is true, you shouldfile a bug:
If you’re using another version of Corona, you should update to the latest your license allows for. Again, if at that point you can’t get this to work file a bug report (with the above code).
Cheers,
ed
hi Roaminggamer,
i use the 2014.2189. The problem, is that i use the corona simulator on linux, so i have this little bug. it don’t worry me.
i have found a lib for a grid module at this adress : https://github.com/nuclearsandwich/corona-game-template/blob/master/docs/grid.md
it’s permit to configure easily a grid and put a touchlistener
for use this you must copy the grid.lua in your coronaprojetc’s folder, below the code of the grid.lua
local gridSquareFunctions = { left = function(gridSquare) if gridSquare.x == 0 then return gridSquare else return gridSquare.grid[gridSquare.y][gridSquare.x - 1] end end, right = function(gridSquare) if gridSquare.x + 1 == gridSquare.grid.xSquares then return gridSquare else return gridSquare.grid[gridSquare.y][gridSquare.x + 1] end end, above = function(gridSquare) if gridSquare.y == 0 then return gridSquare else return gridSquare.grid[gridSquare.y - 1][gridSquare.x] end end, below = function(gridSquare) if gridSquare.y + 1 == gridSquare.grid.ySquares then return gridSquare else return gridSquare.grid[gridSquare.y + 1][gridSquare.x] end end, } local gridFunctions = { eachSquare = function(grid, doEach) for y = 0, grid.ySquares - 1 do for x = 0, grid.xSquares - 1 do doEach(grid[y][x]) end end return grid end, setLocation = function(grid, x, y) grid.displayGroup.x = x grid.displayGroup.y = y return grid end, show = function(grid) grid.displayGroup.isVisible = true end, hide = function(grid) grid.displayGroup.isVisible = false end, } local Grid = {} Grid.newGridRow = function(y) return { y = y } end Grid.newGridSquare = function(grid, y, x) local gridSquare = {} gridSquare.y = y gridSquare.x = x local square = display.newRect(grid.displayGroup, grid.squareSize \* x, grid.squareSize \* y, grid.squareSize, grid.squareSize) gridSquare.displayObject = square gridSquare.left = gridSquareFunctions.left gridSquare.right = gridSquareFunctions.right gridSquare.above = gridSquareFunctions.above gridSquare.below = gridSquareFunctions.below gridSquare.grid = grid return gridSquare end Grid.newGrid = function(xSquares, ySquares, totalWidth) -- Initialize the grid object local grid = {} grid.xSquares = xSquares grid.ySquares = ySquares grid.totalWidth = totalWidth grid.squareSize = totalWidth / xSquares grid.displayGroup = display.newGroup() for y = 0, ySquares - 1 do grid[y] = Grid.newGridRow(y) for x = 0, xSquares - 1 do grid[y][x] = Grid.newGridSquare(grid, y, x) end end grid.eachSquare = gridFunctions.eachSquare grid.setLocation = gridFunctions.setLocation grid.show = gridFunctions.show grid.hide = gridFunctions.hide return grid end return Grid
with this lib you can do a easy touch listener on each cell, there is a typo “function(event)” in his code but i have corrected
local grid = require("grid") myGrid = grid.newGrid(5, 5, 500) local addTouchListener = function(gridSquare) local touch = function(event) if event.phase == "began" then print("Touched square at x: " .. gridSquare.x .. " y: " .. gridSquare.y) end end gridSquare.displayObject:addEventListener("touch", touch) end
but the problem is how to put an image on each cell and this is why I need your help
i have tried this, but the image is only on a cell and not on each cells…have you an idea for this ? According to me it’s just an syntax problem…
myGrid:eachSquare(function(gridSquare) display.newImage("my2.png") end)
good day
hi, i have found the solution
local screenWidth, margins = 360, 1 local grid = require("grid") local grille = grid.newGrid(7, 15, screenWidth - margins) grille:eachSquare(function(gridSquare) gridSquare.displayObject:setFillColor(0, 0, 0, 255) image = display.newImage("my2.png") image.x = gridSquare.displayObject.x image.y = gridSquare.displayObject.y gridSquare.myImage = image end)
Hi.
You can do this:
local myTable = {} myTable.touch = function( self, event ) end Runtime:addEventListener( "touch", myTable )
, but not this:
local myTable = {} myTable.touch = function( self, event ) end myTable:addEventListener( "touch", myTable )
However, if you need to re-direct touch processing for an object to any single object (or table), you can do this:
local myTable = {} myTable.touch = function( self, event ) -- self is table -- event.target is circle you touched end local c1 = display.newCircle( 10, 10, 10 ) local c2 = display.newCircle( 20, 20, 10 ) c1:addEventListener( "touch", myTable ) -- c1 catches touch and send processing to myTable.touch c2:addEventListener( "touch", myTable ) -- same thing for c2
hi, thanks for the reply. i tried what you expected but it’s return that the eventlistener attempt a nil value…
do you know why ?
local myGrid = {} local row = 10 local cell = 15 local rectx = 30 local recty = 30 local gridscaley = 0.35 local gridscalex = 0.35 local space = 2 local spaceleft =10 local spaceright = 50 local image = "myx.png" for i = 1, cell do myGrid[i] = {}; for k = 1, row do myGrid[i][k] = display.newImage(image) myGrid[i][k].yScale=0.35 myGrid[i][k].xScale=0.35 myGrid[i][k].alpha = 1 myGrid[i][k].x = (k - 1) \* (rectx + space) + spaceleft myGrid[i][k].y = (i - 1) \* (recty + space) + spaceright end end myGrid.touch = function ( self, event ) end Runtime:addEventListner( "touch", myGrid )
Are you sure that’s the code you have? If so, the last line has a typo:
Runtime:addEventListner
should be
Runtime:addEventListener
Please try this exact code:
local myTable = {} myTable.touch = function( self, event ) print( self,event.x,event.y) end Runtime:addEventListener( "touch", myTable )
Advice -
Don’t forget to look for ‘typos’ in forums answers. I (and others) often type code without running it. Our answers are right, but may contain typos. A common one is the ‘addEventListner’ typo (missing e).
Actually you did do the right case, I corrected my comment above. Sorry I was in a rush.
sorry for the stupid fault … it would spell checker code in my sublim-text
I receive many details of the grillle I touch with this code but what I would like instead is to receive such
grid[2][5] touched --so that I can do transition.to (character, x = grid[2][5].x, y=grid[2][5].y....)
is that possible?
Sure you can do ‘something’ like that, but keeping your objects in a table to track their <x,y> position isn’t necessary. You’ve already got the <x,y> position of the object stored in the object itself, so have the touch move the object ‘character’ to its position.
Try this small snippet:
local character = display.newCircle( 160, 240, 30 ) character:setFillColor(0,1,0) local onTouch = function( self, event ) if(event.phase == "began") then transition.to( character, { self.x, self.y, time = 1000 } ) end return true end local function makeGridObject( group, x, y, color ) group = group or display.currentStage -- allows this call: makeGridObject( nil, 10, 10, {1,1,1} ) local tmp = display.newCircle( group, x, y, 30 ) tmp:setFillColor(unpack(color)) tmp.touch = onTouch tmp:addEventListener( "touch" ) end local objs = display.newGroup() makeGridObject( objs, 10, 10, {1,1,0} ) makeGridObject( objs, 50, 50, {1,0,1} ) makeGridObject( objs, 200, 200, {0,1,1} )
Corrected typos.
hi, thanks for the code. I have changed some parts (x= self.x, etc), but there is a strange thing : apparently the character doesn’t make the transition exactly a the center. sometimes it’s good and sometimes not. I have put a print self.x and self.y and the value are constantly, so it must move perfectly a the center…Do you know why ?
local character = display.newCircle( 160, 240, 30 ) character:setFillColor(1,0,0) local onTouch = function( self, event ) if(event.phase == "began") then print ( self.x, self.y ) transition.to( character, { x=self.x, y=self.y, time = 1000 } ) end return true end local function makeGridObject( group, x, y, color ) group = group or display.currentStage -- allows this call: makeGridObject( nil, 10, 10, {1,1,1} ) local tmp = display.newCircle( group, x, y, 20 ) tmp:setFillColor(unpack(color)) tmp.touch = onTouch tmp:addEventListener( "touch" ) end local objs = display.newGroup() makeGridObject( objs, 10, 10, {1,1,0} ) makeGridObject( objs, 50, 50, {1,0,1} ) makeGridObject( objs, 200, 200, {0,1,1} )