how to assign an eventlistener on a table ?

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

110914.jpeg

hi, its always the same.

and yet your code is super logic … I do not understand why it no longer bug

@espace3d,

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:

  1. You are using the 2014.2189a
  2. You are using the latest Pro or Enterprise daily build.

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 :slight_smile:

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)