David
David

Reputation: 73

Trying to add a event listener fo a object which is part of an array in lua using corona

The main creates a simple 2d array. Now i want to create a addeventlistener for each object in the table. I presume i do this in the class? Although i have created a taps function and then defined addeventlistener but i ma getting errors.

--main.lua--
grid={}
for i =1,5 do
grid[i]=  {}
for j =1,5 do

        grid[i][j]=diceClass.new( ((i+2)/10),((j+2)/10))
    end
end
--dice class--
local dice = {}
local dice_mt = { __index = dice } -- metatable


function dice.new( posx, posy) -- constructor
local a=math.random(1,6)
local b= true
local newdice = display.newText(a, display.contentWidth*posx,
    display.contentHeight*posy, nil, 60)
--newdice:addEventListener("tap", taps(event))

return setmetatable( newdice, dice_mt )
end


function dice:taps(event)
self.b = false
print("works")
end
function dice:addEventListener("tap", taps(event))

Upvotes: 2

Views: 2138

Answers (2)

brando
brando

Reputation: 313

This stumped me till today. The main problem is that you're making newdice a Corona display.newText object and then reassigning it to be a dice object. All the Corona objects act like ordinary tables, but they're actually special objects. So you have two options:

A. Don't use classes and OOP. As your code is now, there's no reason to have dice be a class. This is the option I'd go with unless you have some compelling reason to make dice a class. Here's how you would implement this option

--dice not a class--
local dice = {}

local function taps(event)
    event.target.b = false
    print("works")
end

function dice.new( posx, posy) -- constructor
    local a=math.random(1,6)
    --local b= true
    local newdice = {}
    newdice = display.newText(a, display.contentWidth*posx,
    display.contentHeight*posy, nil, 60)
    newdice:addEventListener("tap", taps)
    newdice.b = true
    return newdice
end

or B. Use a "has a" relationship instead of an "is a" relationship for the display object. Since you can't make them both a dice object and a display object, your dice object could contain a display object. Here's how that would look.

--dice class--
local dice = {}
local dice_mt = { __index = dice } -- metatable

local function taps(event)
    event.target.b = false
    print("works")
end

function dice.new( posx, posy) -- constructor
    local a=math.random(1,6)
    --local b= true
    local newdice = {}
    newdice.image = display.newText(a, display.contentWidth*posx,
    display.contentHeight*posy, nil, 60)
    newdice.image:addEventListener("tap", taps)
    newdice.b = true
    return setmetatable( newdice, dice_mt )
end

There were some other problems as well. In your taps function event handler you have to use event.target.b instead of self.b. Also, in dice.new b is a local variable so it's not a member of your dice class.

Upvotes: 2

SatheeshJM
SatheeshJM

Reputation: 3633

Remove the last line.

The addEventListener function should be called like this

newdice:addEventListener("tap", taps)

Upvotes: 1

Related Questions