I’ve read a lot of content about using classes in Lua, and I’m still having some trouble to mak it 100% work.
After a lot of research, these two pages (awesome pages) lead me to some code:
http://www.coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/
http://jessewarden.com/2011/10/lua-classes-and-packages-in-corona.html
I’ve made a class to represent a single block (like a gems swapping game), but I can’t understand some things. Here is the code block:
[lua]module(…, package.seeall)
–
– block.lua
– Block object to use as pieces
local block = {}
function block.new( blockType, xPos, yPos ) – constructor
local block = display.newGroup()
local blockImage = display.newImage(“0” … blockType … “.png”)
blockImage:setReferencePoint(display.TopLeftReferencePoint)
block:insert(blockImage)
block.x = xPos;
block.y = yPos;
return block
end
function block:toString()
print( "x = " … block.x … ", y = " … block.y);
end
function block:touch(event)
print(“touch”)
if event.phase == “began” then
self:toString()
return true
end
end
return block[/lua]
and the code of game testing it:
[lua]local block = require( “block” )
local background = display.newRect( 0, 0, 480, 800 ); background:setFillColor(0, 50, 0);
local blocks = {}
– makes a 7x10 matrix and fill with boxes
for i = 1, 7 do
blocks[i] = {}
for j = 1, 10 do
blocks[i][j] = block.new( math.random( 1, 6 ), (i-1)*64+16, (j-1)*64+84 )
blocks[i][j]:addEventListener(“touch”, block)
end
end[/lua]
After the execution, I have a functional matrix filled with boxes. The listeners seems to work (it prints “touch”), but the function toString() did not return the x and y values. Its trows an error or in some modifications nil.
Can anyone give me a tip?
Thanks in advance!
[import]uid: 54349 topic_id: 30372 reply_id: 330372[/import]